JS-filter()笔记
filter()通过条件创建新数组
菜鸟教程JavaScript Array filter() 方法
array.filter(function(currentValue,index,arr), thisValue)
例:获取值>=8的新数组
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const b = a.filter((x) => {
if (x >= 8) {
return x
}
})
console.log(b)//[8,9]
例2:获取值>=8或索引值>=5的新数组
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const b = a.filter((x, index) => {
if (x >= 8 || index >= 5) {
return x
}
})
console.log(b)//[6, 7, 8, 9]