-
-
Save 3rd-Eden/988478 to your computer and use it in GitHub Desktop.
Object merge
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
function m( // named function, for recursion / deeper nested objects | |
a // first object | |
,b // second | |
,c // placeholder | |
){ | |
for(c in b) //loop | |
b.hasOwnProperty(c) && ( // must be own prop | |
(typeof a[c])[0]=='o' ? // if the type is an object. | |
m(a[c],b[c]) : // call the merge function on the object | |
a[c]=b[c] // or just override it | |
) | |
} |
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
function m(a,b,c){for(c in b)b.hasOwnProperty(c)&&((typeof a[c])[0]=='o'?m(a[c],b[c]):a[c]=b[c])} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Arnout Kazemier <blog.3rd-Eden.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "merge", | |
"description": "merge objects", | |
"keywords": [ | |
"merge", | |
"objects", | |
"together" | |
] | |
} |
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
function m(a,b,c){for(c in b)b.hasOwnProperty(c)&&((typeof a[c])[0]=='o'?m(a[c],b[c]):a[c]=b[c])} | |
var FIRST = { | |
1:1 | |
, 2:2 | |
, "three":"three" | |
, "four": [1,2,3,4,5] | |
, "more":{ | |
"five": 5 | |
, "six": "six" | |
, "seven": [1,2,3,4,5,6,7] | |
} | |
}; | |
var moar = { | |
1:2 | |
, 2:3 | |
, "three":"seven" | |
, "four": [4,5,7] | |
, "more":{ | |
"five": 4 | |
, "six": "hello world" | |
, "seven": "pewpew" | |
, "foo":"bar" | |
} | |
}; | |
m(FIRST,moar); | |
console.log(FIRST); |
Author
3rd-Eden
commented
May 25, 2011
via email
The easiest way for recursion checking would be adding each value in the array, and doing a indexOf check on that. But allowing multiple arguments would also be fairly easy to implement.
… that sounds expensive, but if you can pull it off...
i think returning a value and allowing `m({}, objA, objB)` would be the way to go, since folks expect that behavior.
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/988478
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment