定义:
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
注:
(1) filter() 不会对空数组进行检测。
(2)filter() 不会改变原始数组。
语法:
array.filter(function(currentValue,index,arr), thisValue)参数说明:
(1)函数。必选参数,数组中每个元素都会执行这个函数
函数参数:
currentValue :必选参数,当前元素的值。
index: 可选参数,当前元素的索引。
arr: 可选参数,当前元素属于的数组对象。
(2)thisValue 可选参数。对象作为该执行回调时使用,传递给函数,用作this的值,如果省略thisValue,this的值为undedined
实例:
返回数组中所有年龄大于18的元素集合
let sunArr = [{
id:"1",
name:"lee",
age:"20"
},{
id:"2",
name:"sun",
age:"25"
},{
id:"3",
name:"zhao",
age:"16"
}]
newArr = sunArr.filter((item,index,arr) =>item.age >18)
