Created
June 26, 2017 20:28
-
-
Save u01jmg3/f46e71c8aa2be2e36bf9858ab4efe84b to your computer and use it in GitHub Desktop.
Recursively merge properties and return a new object
This file contains 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
/** | |
* Recursively merge properties | |
* and return a new object | |
* `merge(a, b[, ...])` | |
* | |
* @return object | |
*/ | |
function merge() { | |
var dst = {}, | |
src, | |
p, | |
args = [].splice.call(arguments, 0); | |
while (args.length > 0) { | |
src = args.splice(0, 1)[0]; | |
if ($.type(src) === 'object') { | |
for (p in src) { | |
if (src.hasOwnProperty(p)) { | |
if ($.type(src[p]) === 'object') { | |
dst[p] = merge(dst[p] || {}, src[p]); | |
} else { | |
dst[p] = src[p]; | |
} | |
} | |
} | |
} | |
} | |
return dst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment