Last active
August 29, 2015 14:26
-
-
Save iShafayet/71a04635a277ad586e08 to your computer and use it in GitHub Desktop.
Merge two objects/arrays upto any depth, written in coffeescript
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
merge = (left, right) -> | |
return left unless (typeof left is typeof right is 'object') and right isnt null | |
return left unless (Array.isArray left) is (Array.isArray right) | |
ret = if Array.isArray left then [] else {} | |
for key, value of left | |
ret[key] = left[key] | |
for own key, value of right | |
if key of ret | |
ret[key] = merge ret[key], right[key] | |
else | |
ret[key] = right[key] | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment