Created
October 14, 2019 16:54
-
-
Save agrublev/be73015b3f8e3ae5dd960c9469eb3c40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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