I am facing a challenge with two arrays called metaObjects
and justObjects
.
These arrays consist of objects that share a common property called id
.
My goal is to merge properties from the objects in these separate arrays into a new array.
const metaObjects = [
{
id: 1,
metaProp: "metaProp1"
},
{
id: 2,
metaProp: "metaProp2"
}
];
const justObjects = [
{
id: 1,
justProp: "justProp1"
},
{
id: 2,
justProp: "justProp2"
}
];
This is the desired outcome:
const result= [
{
id: 1,
metaProp: "metaProp1",
justProp: "justProp1"
},
{
id: 2,
metaProp: "metaProp2",
justProp: "justProp2"
}
];
I attempted using nested map
functions to achieve this
const combinedObject = justObjects.map(_w => {
return metaObjects.map(_m => {
if (_w.id === _m.id) {
return { ..._m, ..._w };
}
});
}, metaObjects);
console.log(combinedObject);
However, I encountered the following error
[ [ { id: 1, metaProp: 'metaProp1', justProp: 'justProp1' },
undefined ],
[ undefined,
{ id: 2, metaProp: 'metaProp2', justProp: 'justProp2' } ] ]
I am puzzled about why there are undefined
values in the inner arrays.
Additionally, I need to flatten the arrays to align with the expected results provided above.
I have heard about utilizing the composable lens
functions from ramda
Could this approach be beneficial in solving my issue?