- Source:
Methods
merge(base, extensions) → {Object}
Merging objects in JS is easy, using spread operators, as long as we are talking about shallow merging of the first level. This method aims to deep merge recursively, always returning a new object, never touching or changing the original one.
This method implements LIFO precedence: the last extension wins.
Possible differences to other implementations (like lodash's):
- arrays are not concatenated here, but replaced.
- explicitly extending an empty object, replaces the value with the empty object instead of doing nothing
- all involved objects are cloned, so references in the resulting object will differ
Parameters:
Name | Type | Description |
---|---|---|
base |
Object | the object to extend |
extensions |
Array.<Object> | one or more objects to merge into base sequentially, the last taking precedence |
- Source:
Returns:
the (newly created) merged object
- Type
- Object
Example
merge(
{ducks : {uncles : ['Donald', 'Scrooge'], nephews : {huey : true}}},
{ducks : {nephews : {dewey : true}}, mice : ['Mickey']},
{ducks : {uncles : ['Gladstone'], nephews : {louie : true}}, mice : ['Mickey', 'Minnie']}
)
=> {ducks : {uncles : ['Gladstone'], nephews : {huey : true, dewey : true, louie : true}}, mice : ['Mickey', 'Minnie']}