Public
Edited
Dec 25, 2020
Importers
1 star
Insert cell
Insert cell
function deepMerge(obj1, obj2) {
let result;
if (obj2 instanceof Array) {
result = [];
const indicies = Object.keys(obj1)
.concat(Object.keys(obj2))
.sort()
.filter((k, i, arr) => k !== arr[i - 1]);
for (let idx of indicies) {
result[idx] = obj2[idx] || obj1[idx];
}
} else {
result = { ...obj1, ...obj2 };
}

const obj2SharedKeysToObjects = Object.keys(obj2)
.filter(k => typeof obj2[k] === 'object')
.filter(k => typeof obj1[k] === 'object');

for (const key of obj2SharedKeysToObjects) {
result[key] = deepMerge(obj1[key], obj2[key]);
}

return result;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more