I want to merge 2 objects with nested arrays (or subarrays) by taking id
property as key which is present in all the arrays. I tried using _.merge()
and _.mergeWith()
but both of them are either replacing the older object, or extending rather than merging the arrays or they just work on superficial level and doesn't merge deeply nested arrays. Also, merging doesn't mean I want to merge multiple subarrays into single array, but it means merging 2 objects with their respective nested arrays.
Example:
var obj1 = {arr1: [subarr1: [subarr2:[a, b]...]...]...}
var obj2 = {arr1: [subarr1: [subarr2:[a, c]...]...]...}
var obj3 = merge(obj1, obj2)
// obj3 should be {arr1: [subarr1: [subarr2:[a, b, c]...]...]...}