I wanna merge two arrays of objects but I want to skip the objects that has the same ID (i want to save only first object that has same id).
One array is stored locally and the other I'm fetching users from 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"
}
];
I expect to get this. The object in apiUsers with the id: 2 is skipped, because he already exist in the localUsers array of objects. I want to do this to all the objects with the same id.
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"
}
];