题解 | #数组过滤#

数组过滤

https://www.nowcoder.com/practice/46d607f9bf64458daa8798339a7336d0

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
    </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');
            // 补全代码
            select.onchange = function (e) {
                var current = select.value
                var filterData = []
                switch(current) {
                    case '0':
                        break;
                    case '1':
                       filterData = cups.filter(elem => elem.sales < 100)
                        break
                    case '2':
                        filterData = cups.filter(elem => (elem.sales >= 100 && elem.sales < 500))
                        break
                    case '3':
                        filterData = cups.filter(elem => elem.sales > 500)
                        break
                }
                renderDom(filterData)
                
            }
            function renderDom(data = []) {
                var lis = ''
                data.forEach(elem => {
                    lis += `<li>${elem.name}</li>`
                })
                ul.innerHTML = lis
            }
            
            
        </script>
    </body>
</html>

全部评论

相关推荐

10-30 22:18
已编辑
毛坦厂中学 C++
点赞 评论 收藏
分享
努力学习的小绵羊:我反倒觉得这种挺好的,给不到我想要的就别浪费大家时间了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务