I have an array and an object with different properties from the elements of the array, but they are linked together by their common identifier. The array has the following structure:
[{
userId: '12',
prop1: 'blue',
prop2: 'yellow'
},
{
userId: '13',
prop1: 'black',
prop2: 'white'
}]
My object has the following structure:
{id: '13',
prop3: 'heavy',
prop4: 'light'}
I would like to append the object to the array by their given Id, so at the end the array would look like this:
[{
userId: '12',
prop1: 'blue',
prop2: 'yellow'
},
{
userId: '13',
prop1: 'black',
prop2: 'white',
prop3: 'heavy',
prop4: 'light'
}]
As the identifier for the id is not the same, I'm having a hard time to merge them. I have tried merging via lodash using the following code, but haven't had any luck. Thank you!
let merged = _(myArray)
.concat(myObject)
.groupBy('id')
.map(_.spread(_.merge))
.value();