I have two deeply nested objects containing arrays and objects within those arrays. I'd like to merge those. Using 'lodash' it doesn't recognize my objects within arrays.
I have this:
var defaults = {
rooms: [
{
name: 'livingroom',
color: 'white'
},
{
name: 'bedroom',
color: 'red'
}
],
people: {
clothes: [
{
fabric: 'wool',
color: 'black'
},
{
fabric: 'jeans',
color: 'blue'
}
]
}
}
var specific = {
people: {
wigs: [
{
shape: 'trump'
}
]
},
furniture : {
foo: 'bar'
}
}
the result should look like this:
var merged = {
rooms: [
{
name: 'livingroom',
color: 'white'
},
{
name: 'bedroom',
color: 'red'
}
],
people: {
clothes: [
{
fabric: 'wool',
color: 'black'
},
{
fabric: 'jeans',
color: 'blue'
}
],
wigs: [
{
shape: 'trump'
}
]
},
furniture : {
foo: 'bar'
}
}
using lodash
const _ = require('lodash');
console.log(_.merge({}, specific, defaults));
i get
{ people: { wigs: [ [Object] ], clothes: [ [Object], [Object] ] },
furniture: { foo: 'bar' },
rooms:
[ { name: 'livingroom', color: 'white' },
{ name: 'bedroom', color: 'red' } ] }
this is related to:
Merge Array of Objects by Property using Lodash
How to merge two arrays in JavaScript and de-duplicate items
because I don't have a common index or name I'm a bit lost.