I'm trying to find the intersection from 2 different arrays that contain slightly different objects.
For example:
const arr1 = [
{
"number":"1234",
"id":"34782",
"firstName":"John",
"lastName":"Smith",
"email":"test1@test.com",
},
{
"number":"1232",
"id":"34783",
"firstName":"Chad",
"lastName":"Baker",
"email":"test2@test.com",
}
];
const arr2 = [
{
"uuid":"0123",
"firstName":"John",
"lastName":"Smith",
"title":"Director"
},
{
"uuid":"0125",
"firstName":"Sam",
"lastName":"Hurst",
"title":"Manager"
}
]
const arr3 = arr1.filter(object => arr2.includes(object));
console.log(arr3);
I'm trying to create a new array that contains only the objects in arr1
where firstName
and lastName
values are the same in both arrays.
Desired result from above data:
arr3 = [
{
"number":"1234",
"id":"34782",
"firstName":"John",
"lastName":"Smith",
"email":"test1@test.com",
},
]
since this object's firstName and lastName match in both arr1
and arr2
Right now i have this.arr3 = this.arr1.filter(object => this.arr2.includes(object))
But because arr1
and arr2
contain different objects due to proper names, this doesn't work.