<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IFE JavaScript Task 01</title>
</head>
<body>
<h3>污染城市列表</h3>
<ul id="aqi-list">
<!--
<li>第一名:福州(樣例),10</li>
<li>第二名:福州(樣例),10</li> -->
</ul>
<script type="text/javascript">
var aqiData = [
["北京", 90],
["上海", 50],
["福州", 10],
["廣州", 50],
["成都", 90],
["西安", 100]
];
(function () {
/*
在注釋下方編寫代碼
遍歷讀取aqiData中各個城市的數(shù)據(jù)
將空氣質(zhì)量指數(shù)大于60的城市顯示到aqi-list的列表中
*/
var cont=document.getElementById("aqi-list");
var List=new Array();
var j=0;
// 獲取分?jǐn)?shù)大于60的數(shù)組
for(var i=0;i<aqiData.length;i++){
// console.log(aqiData[i][1]);
if(aqiData[i][1] > 60){
List[j]=aqiData[i];
j++;
};
};
// 排序 升序
List.sort(function(x,y){
return x[1]-y[1];
});
//降序
List.reverse();
// 輸出數(shù)組
for(var m=0;m<List.length;m++){
// console.log(List[m]);
var newnode=document.createElement("li");
newnode.innerHTML="第"+(m+1)+"名:"+List[m][0]+",得分:"+List[m][1];
cont.appendChild(newnode);
};
})();
</script>
</body>
</html>
各位好,請問這段代碼中:
a、if(aqiData[i][1] > 60){
List[j]=aqiData[i];
j++;
};
aqiData[i][1]中,中括號里的1該如何理解??
b、 List.sort(function(x,y){
return x[1]-y[1];
});
這里要進(jìn)行排序, x[1]-y[1]這中間的1又要如何理解呢!
先感謝各位?。?!
光陰似箭催人老,日月如移越少年。
The original data given is an array, the 0th item is the city, and the 1st item is the index.
a: aqiData[i] only obtains each sub-array of the array, which is similar to ["Xi'an", 100]. aqiDatai obtains the index before comparison can be made
b: The parameter of the arrangement function in sort is the array List Each value of is also a small array, so you need to get the specific index through X[1] for comparison and reordering
This is a two-dimensional array. The 1 in the square brackets represents the number of the pollution index. For example, [["Beijing", 90]...], the i in your array is 0 at the beginning, so
aqiData[0] represents the first array in the array, which is ["Beijing", 90], so aqiData0 represents "Beijing" and aqiData0 represents 90. The 1 in the latter sorting is the value of the second element in the array. It depends on how you use this function. You should also use it here to compare the level of the pollution index. The parameters passed in should be arrays like ["Beijing", 90], so x[1] and y[1] still refer to the number of the pollution index, which is the same as the previous one. Same.