I have two arrays containing objects like so:
const array1 =
[
{
"id": 1,
"mass": 149,
"height": 180,
"dob": "2003-09-04"
},
{
"id": 2,
"mass": 140,
"height": 175,
"dob": "2000-02-12",
},
{
"id": 3,
"mass": 143,
"height": 170,
"dob": "2001-11-04"
}
]
const array2 =
[
{
"id": 1,
"name": "James",
"sport": "Football"
},
{
"id": 2,
"name": "Adam",
"sport": "Tennis"
}
]
I would like to merge them such that a combined array only contains data where the id appears in both array1 and array2.
So far I'm using lodash _.merge which returns:
const merged = _.merge(array1, array2)
merged = [
{
"id": 1,
"mass": 149,
"height": 180,
"dob": "2003-09-04",
"name": "James",
"sport": "Football"
},
{
"id": 2,
"mass": 140,
"height": 175,
"dob": "2000-02-12",
"name": "Adam",
"sport": "Tennis"
},
{
"id": 3,
"mass": 143,
"height": 170,
"dob": "2001-11-04" ,
}
]
I would just like merged to contain data where id appears in both, i.e:
[{
"id": 1,
"mass": 149,
"height": 180,
"dob": "2003-09-04",
"name": "James",
"sport": "Football"
},
{
"id": 2,
"mass": 140,
"height": 175,
"dob": "2000-02-12",
"name": "Adam",
"sport": "Tennis"
}]
I believe the equivalent function in something like SQL would be an "Inner join"
Any help would be greatly appreciated!