var arrayObj = [
{
animals: {
cat: {
voice: 'meow',
other: {
name: 'Tom'
}
},
dog: {
voice: 'gav'
}
}
},
{
animals: {
cat: {
tail: true
},
dog: {
eyes: 'two'
}
}
}
]
var result = {
animals: {
cat: {
voice: 'meow',
other: {
name: 'Tom',
},
tail: true,
},
dog: {
voice: 'gav',
eyes: 'two',
}
}
}
const merge = (target, ...sources) =>
sources.reduce((acc, n) => (
Object.entries(n).forEach(([ k, v ]) =>
acc[k] = v instanceof Object
? merge(acc[k] instanceof Object ? acc[k] : {}, v)
: v
),
acc
), target);
const result = merge({}, ...arrayObj);