I have two objects (some may call them dictionaries) which look like the following:
object1 =
{
id123:
{
name: Copenhagen,
type: 'city'
}
}
object2 =
{
id123:
{
name: Copenhagen,
type: 'county',
props: { id1: true, id: false}
},
id1234:
{
name: Pars,
type: 'county',
props: { id1: true, id: false}
},
id12345:
{
name: London,
type: 'county',
props: { id1: true, id: false}
},
}
If two ids are the same, my merged object needs to take the type from object1, and needs to keep the props from object2, like so (notice Copenhagen):
mergedObjects =
{
id123:
{
name: Copenhagen,
type: 'city',
props: { id1: true, id: false}
},
id1234:
{
name: Pars,
type: 'county',
props: { id1: true, id: false}
},
id12345:
{
name: London,
type: 'county',
props: { id1: true, id: false}
},
}
I've tried the following with the spread operator:
mergedObjects = { ...object2, ...object1 }
But the whole id object is replaced, rather than merged. Can you help?