Skip to content

Instantly share code, notes, and snippets.

@agrublev
Created October 14, 2019 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrublev/be73015b3f8e3ae5dd960c9469eb3c40 to your computer and use it in GitHub Desktop.
Save agrublev/be73015b3f8e3ae5dd960c9469eb3c40 to your computer and use it in GitHub Desktop.
const isObject = obj => obj === Object(obj);
const merge = (obj1, obj2) => {
Object.keys(obj1).forEach(key => {
if (Array.isArray(obj1[key])) {
if (obj2[key]) obj1[key] = [...obj1[key], ...obj2[key]];
} else if (isObject(obj1[key])) {
if (obj2[key]) obj1[key] = { ...obj1[key], ...obj2[key] };
} else {
if (obj2[key] !== undefined) {
obj1[key] = obj2[key];
}
}
});
return { ...obj2, ...obj1 };
};
merge(
{ thresh: [1, 6], ttee: { one: "A", two: "B" }, testb: 0 },
{ thresh: [51, 55], ttee: { tone: "A", two: "Z" }, testb: true, zzz: true }
); //?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment