I have two array of objects as below:
1st array:
const arr1 = [
{
name: 'Jackson',
email: 'jack@email.com',
_id: '634793ba0d09e246dbed7988'
},
{
name: 'Carlson',
email: 'carl@email.com',
_id: '633c33880d09e246dbed6e3f'
}
]
2nd array:
const arr2 = [
{
name: 'Samson',
email: 'sam@gmail.com',
_id: '"634e878db465120ccc6ca81f'
},
{
name: 'Jennifer',
email: 'jenny@abc.com',
_id: '633c339c0d09e246dbed6eeb'
},
{
name: 'Carlson',
email: 'carl@email.com',
_id: '633c33880d09e246dbed6e3f'
}
]
Now, I want to filter my 2nd array based on the _id
. If same ids, then remove that object from the array.
Expected result should be:
[
{
name: 'Samson',
email: 'sam@gmail.com',
_id: '"634e878db465120ccc6ca81f'
},
{
name: 'Jennifer',
email: 'jenny@abc.com',
_id: '633c339c0d09e246dbed6eeb'
}
]
Below is my approach:
this.finalArray = this.arr2.filter(item => {
this.arr1.forEach(other=> {
if (item._id !== other._id) {
return item;
}
});
});
But I'm getting an empty array. How can I achieve that?