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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more