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.

1

2 Answers 2

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



Sign up to request clarification or add additional context in comments.

Comments

1
{datas && datas.filter(data => filter_ids.includes(data.id))...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.