ES6map⽅法与filter⽅法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后,返回的结果为新的数组
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];
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论