Underscore has several methods for this:
1. _. extend (destination, * sources)
Copy all the properties of the source objects into the target object and return the target object.
_.extend(a, _.extend(b, c)); => {"one" : 1, "two" : 2, "three" : 3, "four" : 4, "five" : 5 }
or
_.extend(a, b); => {"one" : 1, "two" : 2, "three" : 3} _.extend(a, c); => {"one" : 1, "two" : 2, "three" : 3, "four" : 4, "five" : 5 }
2. _. defaults (object, * default)
Fill in the undefined properties in the object with the values from the default objects and return the object .
_.defaults(a, _.defaults(b, c)); => {"one" : 1, "two" : 2, "three" : 3, "four" : 4, "five" : 5 }
or
_.defaults(a, b); => {"one" : 1, "two" : 2, "three" : 3} _.defaults(a, c); => {"one" : 1, "two" : 2, "three" : 3, "four" : 4, "five" : 5 }
gihanchanuka Jun 25 '14 at 9:24 a.m. 2014-06-25 09:24
source share