filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
通过一定的条件逻辑可以筛选过滤去重。
var weathers = [ {
"curTemp" : "35",
"temp" : "22~34",
"weather" : "晴",
"wind" : "东北风4级",
"cname" : "黄帝故里",
"name" : "huangdiguli",
"aqi" : "32",
"aqi1" : "优",
"humidity" : "41%",
"type" : "2"
}, {
"curTemp" : "29",
"temp" : "22~34",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "世纪欢乐园",
"name" : "shijihuanleyuan",
"aqi" : "61",
"aqi1" : "良",
"humidity" : "36%",
"type" : "2"
}, {
"curTemp" : "32",
"temp" : "22~34",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "方特欢乐世界",
"name" : "fangtehuanleshijie",
"aqi" : "42",
"aqi1" : "优",
"humidity" : "46%",
"type" : "2"
}, {
"curTemp" : "29",
"temp" : "22~34",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "城隍庙·文庙",
"name" : "chenghuangmiaowenmiao",
"aqi" : "61",
"aqi1" : "良",
"humidity" : "36%",
"type" : "2"
}, {
"curTemp" : "32",
"temp" : "21~33",
"weather" : "晴",
"wind" : "东北风2级",
"cname" : "中岳庙",
"name" : "zhongyuemiao",
"aqi" : "38",
"aqi1" : "优",
"humidity" : "34%",
"type" : "2"
}, {
"curTemp" : "32",
"temp" : "22~34",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "农业高新科技园",
"name" : "nongyegaoxinkejiyuan",
"aqi" : "42",
"aqi1" : "优",
"humidity" : "46%",
"type" : "2"
}, {
"curTemp" : "29",
"temp" : "22~34",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "郑州海洋馆",
"name" : "zhengzhouhaiyangguan",
"aqi" : "60",
"aqi1" : "良",
"humidity" : "36%",
"type" : "2"
}, {
"curTemp" : "29",
"temp" : "22~34",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "樱桃沟景区",
"name" : "yingtaogoujingqu",
"aqi" : "61",
"aqi1" : "良",
"humidity" : "36%",
"type" : "2"
}, {
"curTemp" : "32",
"temp" : "21~33",
"weather" : "晴",
"wind" : "东北风2级",
"cname" : "嵩阳书院",
"name" : "songyangshuyuan",
"aqi" : "38",
"aqi1" : "优",
"humidity" : "34%",
"type" : "2"
}, {
"curTemp" : "36",
"temp" : "22~33",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "伏羲山神仙洞",
"name" : "fuxishanshenxiandong",
"aqi" : "42",
"aqi1" : "优",
"humidity" : "35%",
"type" : "2"
}, {
"curTemp" : "36",
"temp" : "22~33",
"weather" : "多云",
"wind" : "东北风4级",
"cname" : "伏羲山大峡谷",
"name" : "fuxishandaxiagu",
"aqi" : "40",
"aqi1" : "优",
"wuranwu" : "暂无",
"pm25" : "暂无",
"pm10" : "暂无",
"humidity" : "35%",
"type" : "2"
}, {
"curTemp" : "32",
"temp" : "22~34",
"weather" : "多云",
"wind" : "东北风3级",
"wind6" : "西南风",
"cname" : "古柏渡旅游区",
"name" : "gubaidulvyouqu",
"aqi" : "43",
"aqi1" : "优",
"humidity" : "37%",
"type" : "2"
}, {
"curTemp" : "35",
"temp" : "22~34",
"weather" : "小雨",
"wind" : "东北风4级",
"cname" : "具茨山景区",
"name" : "jucishanjingqu",
"aqi" : "32",
"aqi1" : "优",
"humidity" : "41%",
"type" : "2"
}, {
"curTemp" : "29",
"temp" : "22~34",
"weather" : "晴",
"wind" : "东北风4级",
"cname" : "富景生态世界",
"name" : "fujingshengtaishijie",
"aqi" : "44",
"aqi1" : "优",
"humidity" : "36%",
"type" : "2"
} ];
//过滤晴天的景区
var sunshine = weathers.filter((item) => {return item.weather === '晴'});
//也可以这样写
var sun = weathers.filter(function(item) {
return item.weather === '晴';
});
console.log(sunshine);
console.log(sun);
//数组去重
var language = ['Java','JavaScript','Java','C++','JavaScript'];
var single = language.filter(function(item, index, language) {
//当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
return language.indexOf(item, 0) === index;
});
console.log(single)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)