1

Hi I have something like this that works great:

const handleFavour = () => {
  const { developers } = state;
  const results = developers.filter(list =>
    list.uid.includes("authid")
  );
  setSearchResults(results);
  console.log(results);
};

But now insead of "authid" if i wanted to search using another object or array that has many authids, how do I do this search? Couldn't find anything after searching for a very long time.

1 Answer 1

2

If you want to check if any element in an array matches any element in another array you could combine your current solution with .some.

const handleFavour = () => {
  const allowedIds = ['id1', 'id2'];
  const { developers } = state;
  const results = developers.filter(list =>
    list.uid.some(uid => allowedIds.includes(uid))
  );
  setSearchResults(results);
  console.log(results);
};

Here .some will return true if any of the elements in list.uid are also present in allowedIds.


Based on your situation as described in the comments it looks like you want something like this:

const handleFavour = () => {
  const users = [{ auth: 'id1' }, { auth: 'id2' }];
  const { developers } = state;
  const results = developers.filter(list =>
    users.some(user => user.auth === list.uid)
  );
  setSearchResults(results);
  console.log(results);
};

I initially thought list.uid was an array since you were using .includes, generally I'd recommend using the strict equality operator (===) to compare strings unless you actually want to allow partial matches, but I'm guessing in this case you don't.

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

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.