0

I am building a react app and I am trying to filter an array based on the values of another array.

This is my code:

const filter_ids = [
  "Y5Xtg3T2V4aYHilhU0c6", 
  "DeVpCBd98yXksB5NM4tw"
]

{datas && datas.filter(data => data.id  === filter_ids)
    .map(data =>{
    return (
      <ul>
        <li>data</li>
      </ul>
    )
  })}

I would like to have it such that if an object from the datas array has an ID that matches any one of the values on the filter_ids, it returns that object. When I do it like this, it doesnt work.

Please help.

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
Sky Lurk
  • 417
  • 1
  • 3
  • 13
  • 1
    Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Robin Zigmond May 06 '21 at 21:42

2 Answers2

1
{datas && datas.filter(data => filter_ids.includes(data.id))...
Bafsky
  • 761
  • 1
  • 7
  • 16
1

Use Array#includes


const filter_ids = [
  "Y5Xtg3T2V4aYHilhU0c6", 
  "DeVpCBd98yXksB5NM4tw"
]

{datas && datas.filter(data =>filter_ids.includes(data.id))
    .map(data =>{
    return (
      <ul>
        <li>data</li>
      </ul>
    )
  })}



JustRaman
  • 1,101
  • 9
  • 11