以下代码,使用filter()方法的item参数过滤掉了数组中的数据“2”和“3”。(item为当前元素的值)
var array = [1,2,3,4,5]
var result = array.filter((item)=>{
return item !== 2 && item!== 3
})
console.log(result)
//(3) [1, 4, 5]
使用arr参数过滤掉了数组中的第二条和第三条数据,效果与上面相同。(arr为当前的数组)
var array = [1,2,3,4,5]
var result = array.filter((item,index,arr)=>{
return item !==arr[1] && item !== arr[2]
})
console.log(result)
//(3) [1, 4, 5]
使用index参数过滤了索引值为1和2的数据,实现了同样的效果。(index为当前元素的索引值)
var array = [1,2,3,4,5]
var result = array.filter((item,index,arr)=>{
return item =(index !== 1 && index !== 2)
})
console.log(result)
//(3) [1, 4, 5]