<!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中各个城市的数据
将空气质量指数大于60的城市显示到aqi-list的列表中
*/
var cont=document.getElementById("aqi-list");
var List=new Array();
var j=0;
// 获取分数大于60的数组
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();
// 输出数组
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];
});
这里要进行排序, 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.