作用:

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

语法:

array.filter(function(currentValue,index,arr), thisValue)
  • currentValue,必填,当前元素的值
  • index,可选,当前元素在数组中的索引值
  • arr可选,当前元素属于的数组对象
  • thisValue,可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
    如果省略了 thisValue ,"this" 的值为 "undefined"

注意:

filter() 不会对空数组进行检测。
filter() 不会改变原始数组。

实例:

let ages = [33,44,55,66,77]; 
ages.filter((item)=>{return item>18})
打印结果[33, 44, 55, 66, 77]

拓展:

可以用来做删除数组元素的操作
let ages = [33,44,55,66,77]; 
ages.filter((item)=>{return item !== 55})
打印结果[33, 44, 66, 77]

William_Wang
21 声望1 粉丝