I have two arrays of objects, the objects within these arrays have a slightly different keys but share same id.
What i am trying to do is merge these two arrays into one that contains all the elements from both without duplicates.
array one:
[
{id: 'ABC_123', value: 10, label: 'tree', level: 'mid'},
{id: 'ABC_456', value: 11, label: 'grass', level: 'top'}
];
array two:
[
{levelId: 'ABC_123', value: 10, label: 'tree', level: 'mid'},
{levelId: 'ABC_456', value: 11, label: 'grass', level: 'top'},
{levelId: 'ABC_789', value: 11, label: 'bush', level: 'low'}
];
I want to bring all the new elements from array two into array one in this example i want element with levelId: 'ABC_789
to move into array one and change the levelId key into id:
result:
[
{id: 'ABC_123', value: 10, label: 'tree', level: 'mid'},
{id: 'ABC_456', value: 11, label: 'grass', level: 'top'}
{id: 'ABC_789', value: 11, label: 'bush', level: 'low'}
]
I have tried mapping over them matching the id’s and creating new array but I cant get the desired result
4
Answers
You could collect the
id
values from the first array, but them in a set, and iterate the second collection to select those that have no match in that set. With destructuring assignment you can achieve the change oflevelId
toid
:You need to loop over both structures.
I am using reduce as a loop structure and find acts as one too.
You essentially want to look through the possible new values and determine if it exists already in the first array. If it doesn’t exist then push its value into array 1.. in this case a new object structure.
Here is a rough solution:
I would just initiate the result with
arr1
, then for each element inarr2
check if already exist. If yes – ignore it, otherwise append to the result and changelevelId
toid
:Assuming the length of the
array1
isn
and length ofarr2
ism
, this solution complexity is:Run time:
O(m)
Space:
O(n)
You can use the map function that is part of the array in JS.