0

I have two Array called users and invited users

const users = [ {name:'dfd', email: 'gd12@gmail.com'},{name:'fgf', email: 'gfgf12@gmail.com'}]
const invitedUser= [ {name:'aaa', email: 'gd12@gmail.com'},{name:'aaa', email: 'rt@gmail.com'}]

Now I want to filter out the object when the email of a user matches the email of the invited user. For example:

// if -- users.email === invitedUser.email

I know it can be done by using the filter method but here both are arrays and I never worked with two array at the same time and compare the value. any solution

  • 3
    Does this answer your question? [Filter array of objects with another array of objects](https://stackoverflow.com/questions/31005396/filter-array-of-objects-with-another-array-of-objects) – evolutionxbox Mar 16 '21 at 17:06
  • When looking at the question that evolutionxbox linked, be sure to read the [most upvoted answer](https://stackoverflow.com/a/31005753/17300), which is better than the accepted answer. – Stephen P Mar 16 '21 at 17:21

2 Answers2

1

Array.prototype.filter is your friend.

const users = [{
  name: 'dfd',
  email: 'gd12@gmail.com'
}, {
  name: 'fgf',
  email: 'gfgf12@gmail.com'
}];

const invitedUsers = [{
  name: 'aaa',
  email: 'gd12@gmail.com'
}, {
  name: 'aaa',
  email: 'rt@gmail.com'
}];

const overlap = users.filter(user => invitedUsers.some(invite => user.email === invite.email));

console.log(overlap);
Jonathan
  • 8,771
  • 4
  • 41
  • 78
0
const invented = users.filter(isUserInvented);

function isUserInvented(user){
       return invitedUser.find(u=> u.email == user.email)
}