To merge array of objects by property using JavaScript Lodash, we use the unionBy
function.
For instance, we write
const original = [
{ label: "private", value: "[email protected]" },
{ label: "work", value: "[email protected]" },
];
const update = [
{ label: "private", value: "[email protected]" },
{ label: "school", value: "[email protected]" },
];
const result = _.unionBy(update, original, "label");
to call unionBy
with the update
and original
arrays and merge the objects into one by matching the label
property.
A new array with the merged objects is returned.