JS Task2

作者: Hathaway_桉 | 来源:发表于2017-06-09 09:56 被阅读0次
    图片.png
    var list=document.getElementById('aqi-list');
           var arr1=new Array();
           for(var i=0;i<aqiData.length;i++){
               if(aqiData[i][1]>60){
                   arr1.push(aqiData[i]);
               }
           }
           //console.log(arr1);
           for(var i=0;i<arr1.length;i++){
               var newNode=document.createElement('li');
               newNode.innerHTML=arr1[i][0]+":"+arr1[i][1];
               list.appendChild(newNode);
           };
    
    
    
    

    比较好的答案:

    var cont=document.querySelector("#aqi-list");
            var list=aqiData.filter(function(item,index,array){
                return(item[1]>=60);
            });
            list.sort(function(a,b){
                return b[1]-a[1];
            });
            list.forEach(
                    function(item,index,array){
    
                        var newnode=document.createElement("li");
                        newnode.innerHTML=array[index][0]+":"+array[index][1];
                        cont.appendChild(newnode);
    
                    }
            );
    
    

    总结:
    http://ife.baidu.com/note/detail/id/694
    练习写ES6的写法:

     var list=document.getElementById('aqi-list');
           var arr1=new Array();
           for(var i=0;i<aqiData.length;i++){
               if(aqiData[i][1]>60){
                   arr1.push(aqiData[i]);
               }
           }
    

    转换成:

    let list=document.getElementById('aqi-list');
           let arr1=new Array();
           const temp = Array.from(aqiData);
           temp.forEach((item,index) => {
            if(item[1] > 60){
              arr1.push(item)
            }
           })
    

    相关文章

      网友评论

          本文标题:JS Task2

          本文链接:https://www.haomeiwen.com/subject/kmptqxtx.html