代码拉取完成,页面将自动刷新
<!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"><100</option>
<option value="2">100~500</option>
<option value="3">>500</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>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。