关于javascript:filter方法

4次阅读

共计 369 个字符,预计需要花费 1 分钟才能阅读完成。

作用:

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]
正文完
 0