java script遍历数组的应用

商品的查询

// An highlighted block
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    *{
        margin: 0;
        padding: 0;
    }
    table{
        border-collapse: collapse;
        margin: 0 auto;
        margin-top: -1px;
    }
   td ,th{
        border: 1px solid #000;
        padding: 0 50px;
        width: 80px;
        height: 40px;
        line-height: 40px;
        text-align: center;
    }
    tr{
        border: 1px solid #000;
    }
    </style>
</head>

<body>
    <div class="search">
        按照价格查询:<input type="text" class="start">- <input type="text" class="end">
        <button class="btn">搜索</button> 按照商品名称查询:<input type="text" class="product">
        <button class="search-pro">查询</button>
    </div>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>商品名称</th>
                <th>价格</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <script>
    var data=[
        {
            id:1,
            pname:'小米',
            price:3999

        },
        {
            id:2,
            pname:'oppo',
            price:999

        },
        {
            id:3,
            pname:'荣耀',
            price:1299

        },
        {
            id:4,
            pname:'华为',
            price:1999

        }
    ];
    var tbody=document.querySelector('tbody');
    var btn=document.querySelector('.btn');
    var start=document.querySelector('.start');
    var end=document.querySelector('.end');
     setDate(data);
    function setDate(mydate){
        //渲染之前清空数据
        tbody.innerHTML='';
        mydate.forEach(function(value){
        // console.log(value);
        var tr=document.createElement('tr');
        tr.innerHTML='<td>'+value.id+'</td><td>'+value.pname+'</td><td>'+value.price+'</td>';
        tbody.appendChild(tr);
    });
    }
    btn.addEventListener('click',function(){
         var newDate=data.filter(function(value){
            return value.price>=start.value&&value.price<=end.value;
        })
        // console.log(arr);
        //筛选完重新渲染一次页面
        setDate(newDate);
        
    });
    var search_pro=document.querySelector('.search-pro');
    var product=document.querySelector('.product');
    search_pro.addEventListener('click',function(){
        var arr=[];
        // 当符合某种条件时选择some 比较方便 使用some前先声明一个数组
        data.some(function(value){
          if(value.pname===product.value){
            // console.log(value);
            // 点击按钮以后返回的是一个对象 我们遍历的时候必须是数组
            arr.push(value);
            return true;
          }
         
        }); 
        setDate(arr);
    });
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值