js 数组过滤器filter
记录一下数组过滤器得用法
过滤器会创建一个新的数组,原数组不会改变
语法:新数组 = 非空原数组.filter((条目,索引,原数组)=>{
筛选得业务逻辑
})
eg:
let oldArr = [ { name:'tom', age:13 }, { name:'tom2', age:14 }, { name:'tom3', age:15 }, { name:'tom4', age:16 }, ]; let newArr = oldArr.filter((item, index,arr)=>{ return item.age>15 }); console.log(oldArr.length) //4 console.log(newArr.length) //1