lutils-merge
merge Merge javascript objects recursively.
npm install lutils-merge
API
merge
, merge.white
, merge.black
supports these patterns:
merge([target], [object], [...[object]])
Merges all objects into first object.
const obj1 = a: 1 b: c: 1 e: 3 const obj2 = a: 2 b: d: 2 const obj3 = {} // Merges objects obj2 then obj3 into obj1/* { a: 2, b: { c: 1, d: 2 }, e: () => {} }*/ // Use options to customize merge behaviour.// The first argument is an array of objects,// and the second is the options object./* { a: { b: 2 }, d: 1 }*/
merge.white()
Merges all objects into first object only if there is a key match.
merge/* { a: 2 }*/
merge.black()
Merges all objects into first object only if there isn't a key match.
merge/* { a: 1, b: 2 }*/
Advanced usage
Options
// Decremented with each recursion for each nested object, halting the merge at 0 depth: 8 // Determines whether recursing will occur. When this type matches, it will be iterated over. types: object: true array: true types: "object" "array" // Can also be an array of type strings // See "Test functions" below {}
Test Functions
When merging, a test function can be specified in two ways:
The test function is supplied paramaters for each iteration through the merge.
For example; merge.white
is defined with this test function:
{ if paramsrecursing return true return paramskey in paramsobj2}