javascript - 如何在 React 中过滤屏幕上显示的数组项?

标签 javascript react-native

我正在学习 React 基础知识,并遇到了这个给我带来很多麻烦的挑战。

我有“状态”功能中的项目列表。当我在屏幕上列出它们时,我想列出具有“found=0”值的那些。我尝试在网络中搜索,尝试应用很多不同的解决方案,包括 stackoverflow 中的解决方案,但我不断收到很多不同的错误,所以我无法使其工作。

这是我保存在 App.js 文件中的“状态”函数:

  state = {
counters: [
  { id: 1, title: "Soymilk", found: 1, value: 0 },
  { id: 2, title: "Mushroom", found: 0, value: 0 },
  { id: 3, title: "Tomatoes", found: 1, value: 0 },
  { id: 4, title: "Potatoes", found: 0, value: 0 },
  { id: 5, title: "Meat", found: 0, value: 0 },
  { id: 6, title: "Beans", found: 0, value: 0 },
  { id: 7, title: "Bread", found: 0, value: 0 }
]};

这是显示我的项目的 counter.jsx 代码:

  render() {
return (
  <div className="list-group">
    <span className="font-weight-bold list-group-item list-group-item-action flex-column align-items-start">
      {this.props.counter.title}
      <span className="font-weight-normal text-secondary float-right">
        {this.foundMessage()}
      </span>
    </span>
    <div className="row">
      <button
        onClick={() => this.props.onFind(this.props.counter)}
        className="btn btn-sm col-3 btn-primary col-6"
      >
        Found it!
      </button>
      <button
        onClick={() => this.props.onDelete(this.props.counter.id)}
        className="btn btn-danger btn-sm col-6"
      >
        No more needed!
      </button>
    </div>
  </div>
);}}

最佳答案

您可以过滤数据,

像这样,

let filteredData= this.state.counters.filter(item=>item.found==0);
this.setState({counters:filteredData});

关于javascript - 如何在 React 中过滤屏幕上显示的数组项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55740083/

相关文章:

javascript - 使用 Algolia 进行地理搜索

javascript - Cordova 后退按钮不起作用

JavaScript 媒体查询破坏 greensock

ios - AppDelegate 使用 Firebase 更新

reactjs - 您在 <ScrollView> 上指定了 `onScroll` 但不是 `scrollEventThrottle`

javascript - 如果使用 UI-Bootstrap 的 typeahead 指令未找到匹配项,则表单不可编辑?

javascript - 将用户点击推送到数组,然后在 Javascript 中设置时间后显示数组?

ios - 我想安装 expo-cli,但是有错误。并且无法卸载 expo-cli

react-native - react 本地Google日历

animation - 在项目后对 native 动画平面列表项目使用react