题解 | #无重复数组#

无重复数组

https://www.nowcoder.com/practice/d2fa3632268b41df9bc417b74802ad8c

        <script>
            var cups = [
                { type: 1, price: 100, color: 'black', sales: 3000, name: '牛客logo马克杯' },
                { type: 2, price: 40, color: 'blue', sales: 1000, name: '无盖星空杯' },
                { type: 4, price: 60, color: 'green', sales: 200, name: '老式茶杯' },
                { type: 3, price: 50, color: 'green', sales: 600, name: '欧式印花杯' }
            ]
            var ul = document.querySelector('ul');
            var upbtn = document.querySelector('.up');
            var downbtn = document.querySelector('.down');
            // 补全代码
    upbtn.onclick = function(){
      cups.sort((a,b)=>{
        return a.sales - b.sales
      })
      showLi()
    }
    downbtn.onclick = function(){
      cups.sort((a,b)=>{
        return b.sales - a.sales
      })
      showLi()
    }
    function showLi() {
      ul.innerHTML = ''
       for (let i = 0; i < cups.length; i++) {
      ul.innerHTML += `<li>${cups[i].name}</li>\n`
    }
    }
            
        </script>
    </body>
    
    全部评论

    相关推荐

    点赞 收藏 评论
    分享
    牛客网
    牛客企业服务