【问题标题】:How to use filter in react?如何在反应中使用过滤器?
【发布时间】:2019-04-20 10:38:00
【问题描述】:

我正在使用以太坊和 react 构建签到应用程序。

checkinplaceidusername,我想按id过滤数组。

现在,我的代码是这样的

{ this.props.checkins.map((checkin, key) => {
  return(
    <div key={key}>
      <p>ID{checkin.placeid}, Address:{checkin.username}</p>
    </div>
  )
})}

请给我一些建议。

【问题讨论】:

标签: reactjs ethereum


【解决方案1】:

使用.filter(),您可以根据条件过滤数组,该函数返回满足条件的项目数组。

filter() 方法创建一个数组,其中填充了所有通过测试的数组元素(作为函数提供)。

const filtered = this.props.checkins.filter(item => item.placeid === someid);

{ filtered.map((checkin, key) => {
  return(
    <div key={key}>
      <p>ID{checkin.placeid}, Address:{checkin.username}</p>
    </div>
  )
 });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2020-05-27
    • 2021-09-10
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多