关于javascript:filter方法

作用:

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]

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理