参考答案:
首先,map和filter函数的参数,是完全相同的
array.map(function(currentValue,index,arr), thisValue)
array.filter(function(currentValue,index,arr), thisValue)
但是在用途上,它们是有区别的:
示例:
let arr = ["1","2","3"];
let a = arr.map((item,index,a) =>{
return item + 1
});
console.log(a);//["11", "21", "31"]
let b = arr.filter((item,index,a) =>{
return item > 1
})
console.log(b);//["2", "3"]
另外,filter可过滤NaN、null、undefined、0
1let arr = [NaN,null,undefined,"0",0,1,2,3]; 2let newArr = arr.filter(item => item); 3console.log(newArr);//["0", 1, 2, 3]
最近更新时间:2024-08-10