What is lodash.mergewith?
The lodash.mergewith package is a utility library that provides a method for deep merging objects. It allows for custom merge functions to be specified, enabling more complex merging strategies than the default deep merge.
Deep Merging Objects
This feature allows for deep merging of objects with a custom merge function. In this example, arrays are concatenated instead of being replaced.
const _ = require('lodash.mergewith');
const object = { 'a': [{ 'b': 2 }, { 'd': 4 }] };
const other = { 'a': [{ 'c': 3 }, { 'e': 5 }] };
function customizer(objValue, srcValue) {
if (Array.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
const result = _.mergeWith(object, other, customizer);
console.log(result); // { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }