<!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 newDate = aqiData.sort(function(a,b){
return b[1] - a[1];
});
//alert(newDate);
var newarr = newDate.filter(function(value){
if(value[1] > 60){
return value[1];
}
})
//alert(newarr);
var getul = document.querySelector('#aqi-list');
for(var i = 0;i < newarr.length;i++){
var li = document.createElement('li');
li.innerHTML = "第"+(i+1)+"名"+":"+newarr[i][0]+","+newarr[i][1];
getul.appendChild(li);
}
})();
</script>
</body>
</html>用代码来说服我,谢谢
2 回答
已采纳
千秋此意
TA贡献158条经验 获得超187个赞
<!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() { var getul = document.querySelector('#aqi-list'); var newarr = aqiData.filter(item => (item[1] > 60)).sort((a, b) => (b[1] - a[1])); newarr.forEach(function(item, idx) { getul.innerHTML += `<li>第${idx + 1}名: ${item[0]}, 分数: ${item[1]}</li>`; }); })(); </script> </body> </html>
反正都用了es6不妨多用点儿~
添加回答
举报
0/150
提交
取消