JS Task2

2017-06-09  本文已影响0人  Hathaway_桉
图片.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)
        }
       })
上一篇下一篇

猜你喜欢

热点阅读