I have two arrays of objects that represent email addresses that have a label and a value:
var original = [
{
label: \'private\',
value: \'private@
Convert the lists to objects keyed by label, merge them by _.assign, and convert it back to an array. It will even retain order of the items on most browsers.
var original = [
{
label: 'private',
value: 'private@johndoe.com'
},
{
label: 'work',
value: 'work@johndoe.com'
}
];
var update = [
{
label: 'private',
value: 'me@johndoe.com'
},
{
label: 'school',
value: 'schol@johndoe.com'
}
];
console.log(
_.map(
_.assign(
_.mapKeys(original, v => v.label),
_.mapKeys(update, v => v.label)
)
)
);
// or remove more duplicated code using spread
console.log(
_.map(
_.assign(
...[original, update].map(
coll => _.mapKeys(coll, v => v.label)
)
)
)
);