map 方法:可以简单的理解为映射
var num = [1,2,3,4];
var dataAdd = num.map(n => n+n);
var datadeep = num.map(n => n-1);
console.log(dataAdd);//[2, 4, 6, 8]
console.log(datadeep);//[0,1,2,3]
filter方法: 过滤筛选的作用,数组filter后,返回的结果为新的数组
var chinaWords = [{
name:"君住长江头",
msg:"诗词",
age:15
},{
name:"我住长江尾",
msg:"歌赋",
age:15
},{
name:"日日思君不见君,同饮一江水",
msg:"古风",
age:14
}]
var dataname = chinaWords.map(one => one.name)
console.log(dataname.join(','));//君住长江头,我住长江尾,日日思君不见君,同饮一江水
var oneages = chinaWords.map(one => one.age)
console.log(oneages.join(','));//15,15,14
var ageJudge = oneages.filter(age => age > 14 )
console.log(ageJudge.join(','));//15,15
filter() 去掉空字符串、undefined、null
let arr = ['','1','2',undefined,'3.jpg',undefined]
let newArr = arr.filter(item => item)
console.log(newArr)
filter()数组去重
let array = [1,3,5,5,7,7,7,8,9];
function unique(arr) {
return arr.filter(function(item, index, arr) {
return arr.indexOf(item, 0) === index;
});
}
unique(array);//[1,3,5,7,8,9];