javascript - 过滤并删除数组

标签 javascript reactjs

我有一个过滤器方法,可以检查 result 是否不包含 this.state.filter 中的值,应将其删除。

我的问题是方法没有删除错误的结果。它使 null

filter(){
    const {fetchData} = this.state;
  return  fetchData.map((result, index) => (
      (result[3] == this.state.filter) ?
      result :  null //there is my problem
    ))
  }

来自console.log的屏幕

before

after

最佳答案

您必须使用 Array.prototype.filter 而不是 map 来从数组中删除元素:

filter() {
  const { fetchData } = this.state;
  return fetchData.filter( (subarray) => (
    subarray[3] === this.state.filter
  ));
}

关于javascript - 过滤并删除数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754262/

相关文章:

javascript - 没有目标 ="blank"?

PHP - JS 和 CSS 压缩器

reactjs - 在 Mutation 组件之外访问 Apollo 的加载 bool 值

javascript - 如果我没有向 URL 提供参数值,如何呈现 Page Not Found.js?

reactjs - 如何从 React 发送 CORS 请求?

javascript - Gatsby / react : how to create clean fallback for non JS users?

JavaScript 错误 :Object doesn't support this property or method in IE Browser

php - 动态更改iframe的src内容

javascript - 回顾:Meteor-seo 坏了?

node.js - 在 axios header 上设置默认 header 是否会反射(reflect)在其他模块中所有导入的 axios 实例中?