js-merge-object
Utility method to merge nested JS objects recursively.
Description
This lightweight util method will merge objects; nested and if required recursively.
// { a: { c: 2, b: 3, k: 5 }, c: 3 }
To add a flavour, this method will handle arrays as a special case.
If merge encounters,
- Two arrays with the same key, if arrays have similar types arrays will be concatenated.
- An array and non-array element found with the same key, if type of non-array is similar to type of elements in the array, non-array element will be pushed to the array.
;// { a: [1, 2, 5, 7] } -> Arrays with same types will concat ;// { a: [1, 2, 5 ] } -> Item will be pushed to an array with same type
Add this to your application
npm i js-merge-object
How to use
const merge = ; ;// {a: { b: 2, c: 4 } } -> Same key object are merged ;// { a: 3 } -> Key found at last will override others ;// { a: [1, 2, 5, 7] } -> Arrays with same types will concat