1 Star 0 Fork 0

癫疯丶歌谣/前端牛客网面试题刷题

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
第九十五题 数组过滤.html 2.50 KB
一键复制 编辑 原始数据 按行查看 历史
癫疯丶歌谣 提交于 2022-07-05 17:35 . 修改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数组过滤</title>
</head>
<body>
<select name="" id="">
<option value="0">请选择销量范围</option>
<option value="1">&lt100</option>
<option value="2">100~500</option>
<option value="3">&gt500</option>
</select>
<ul>
<li>牛客logo马克杯</li>
<li>无盖星空杯</li>
<li>老式茶杯</li>
<li>欧式印花杯</li>
</ul>
<script>
var cups = [{
type: 1,
price: 100,
color: 'black',
sales: 60,
name: '牛客logo马克杯'
},
{
type: 2,
price: 40,
color: 'blue',
sales: 100,
name: '无盖星空杯'
},
{
type: 4,
price: 60,
color: 'green',
sales: 200,
name: '老式茶杯'
},
{
type: 3,
price: 50,
color: 'green',
sales: 600,
name: '欧式印花杯'
}
]
var select = document.querySelector('select');
var ul = document.querySelector('ul');
// 补全代码
var cups = [{
type: 1,
price: 100,
color: 'black',
sales: 60,
name: '牛客logo马克杯'
},
{
type: 2,
price: 40,
color: 'blue',
sales: 100,
name: '无盖星空杯'
},
{
type: 4,
price: 60,
color: 'green',
sales: 200,
name: '老式茶杯'
},
{
type: 3,
price: 50,
color: 'green',
sales: 600,
name: '欧式印花杯'
}
]
var select = document.querySelector('select');
var ul = document.querySelector('ul');
// 补全代码
select.onchange = function () {
var index = select.selectedIndex;
function show(arr) {
let list = '';
arr.forEach(function (v) {
list += `<li>${v.name}</li>`
})
ul.innerHTML = list;
}
function choice(cups, index) {
let resArr = cups.filter(function (v) {
if (index == 1) {
return v.sales < 100;
} else if (index == 2) {
return v.sales >= 100 && v.sales <= 500;
} else if (index == 3) {
return v.sales > 500;
}
})
return resArr;
}
show(choice(cups, index));
}
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/geyaoisgeyao/geyao.git
git@gitee.com:geyaoisgeyao/geyao.git
geyaoisgeyao
geyao
前端牛客网面试题刷题
master

搜索帮助