documentcloud/underscore — Issue #434: Feature: join two collections
Author: jasonkuhrt
Created: 2012-01-15 03:16:08+00:00
State: open

I propose a function for joining two collections. Below is an example implementation:

joinMany = (arr_obj)->
obj1 = {}
join( obj1, obj2 ) for obj2 in arr_obj
obj1

join = (obj1, obj2, overwrite_primitives=no)->
for key, value of obj2
if not hasValue(obj1[key])
obj1[key] = value
else if typeof obj1[key] is "object"
join(obj1[key], obj2[key])
else
if overwrite_primitives is true
obj1[key] = value
else
# do nothing
obj1

I think this would be a handy addition to underscore. The feature is pretty self explanatory but if there's any confusion let me know. I have a feeling the implementation can be improved a lot. This is just a first draft!