题解 | #数组过滤#
数组过滤
https://www.nowcoder.com/practice/46d607f9bf64458daa8798339a7336d0
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 (e) { let option = this.value; let arr = []; if (option === "1") { arr = cups.filter((item) => { return item.sales < 100; }); } else if (option === "2") { arr = cups.filter((item) => { return item.sales >= 100 && item.sales <= 500; }); } else { arr = cups.filter((item) => { return item.sales > 500; }); } show(arr); }; function show(array) { ul.innerHTML = ""; array.forEach((item) => { let li = document.createElement("li"); li.innerText = item.name; ul.append(li); }); }