Search code examples
reactjsethereum

How to use filter in react?


I am building check-in app by using ethereum and react.

checkin have placeid and username, and I want to filter the array by id.

Now, my code is like this

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

Could you give me any advise, please.


Solution

  • using .filter() you can filter your array based on a condition, the function returns an array of items that satisfy the condition.

    The filter() method creates an array filled with all array elements that pass a test (provided as a function).

    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>
      )
     });
    }