const first = [
{ name: 'first', value: '' },
{ name: 'second', value: 10 },
]
const second = [
{ name: 'first', value: 0 },
{ name: 'third', value: 20 }
]
const third = [
{ name: 'first', value: 0 },
{ name: 'second', value: 10 },
{ name: 'third', value: 20 }
]
name
, а значение – весь объект с name
и value
. Перезаписывать предыдущий с таким же name
или нет – по условию;const uniq = (a, b) => Object.values(
a.concat(b).reduce(
(acc, o) => {
if (!acc[o.name]) acc[o.name] = o;
else if (Number.isFinite(o.value)) acc[o.name].value = o.value;
return acc;
}, {}
)
);
//###########################
const first = [{ name: 'first', value: '' }, { name: 'second', value: 10 }];
const second = [{ name: 'first', value: 0 }, { name: 'third', value: 20 }];
/*
[
{
"name": "first",
"value": 0
},
{
"name": "second",
"value": 10
},
{
"name": "third",
"value": 20
}
]
*/
//###########################
const first = [{ name: 'first', value: 100 }, { name: 'second', value: 0 }];
const second = [{ name: 'first', value: -111 }, { name: 'second', value: '' }];
/*
[
{
"name": "first",
"value": -111
},
{
"name": "second",
"value": 0
}
]
*/
_.unionBy([2.1], [1.2, 2.3], Math.floor);
// => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]