JS Task2

weixin_33785972發表於2017-06-09
5623231-4806ebc644d01cd7.png
圖片.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)
        }
       })

相關文章