I have two arrays of objects that I need to merge, but I want to exclude any objects with duplicate IDs (I only want to keep the first object with each ID).
One array is stored locally, and the other is retrieved from an API.
const localUsers = [
{
"id": 1,
"first_name": "Adam",
"last_name": "Bent",
"avatar": "some img url"
},
{
"id": 2,
"first_name": "New Name",
"last_name": "New Last Name",
"avatar": "some new img url"
}
];
const apiUsers = [
{
"id": 2,
"first_name": "Eve",
"last_name": "Holt",
"avatar": "some img url"
},
{
"id": 3,
"first_name": "Charles",
"last_name": "Morris",
"avatar": "some img url"
}
];
The expected output should be skipping the object in apiUsers with the id: 2, as it already exists in the localUsers array. This exclusion should apply to all matching IDs.
const mergedUsers = [
{
"id": 1,
"first_name": "Adam",
"last_name": "Bent",
"avatar": "some img url"
},
{
"id": 2,
"first_name": "New Name",
"last_name": "New Last Name",
"avatar": "some new img url"
},
{
"id": 3,
"first_name": "Charles",
"last_name": "Morris",
"avatar": "some img url"
}
];