Skip to content

Instantly share code, notes, and snippets.

@iShafayet
Last active August 29, 2015 14:26
Show Gist options
  • Save iShafayet/71a04635a277ad586e08 to your computer and use it in GitHub Desktop.
Save iShafayet/71a04635a277ad586e08 to your computer and use it in GitHub Desktop.
Merge two objects/arrays upto any depth, written in coffeescript
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