js报错 Cannot read property 'filter/map' of undefined

JavaScript Array filter() 方法

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

要注意的地方:

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

也就是说 如果不能确定这个指定数组的数据格式(可能为 null 等),那么可以这么来写:

var themes = (arr || []).filter(function (item) {
    return item > 0;
})

map 等迭代方法同理。

相关文章:

  • 2021-06-06
  • 2022-12-23
  • 2021-12-22
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2021-08-28
猜你喜欢
  • 2021-04-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
相关资源
相似解决方案