I need to get the expectedOutput array, which consists of the objects with the higher amount number. This code concat the two arrays and then reduce by higher amount. Im looking for a better way to do this, without concat. Thanks in advance.
let arr1 = [{name: 'Almendras', amount: 0},{name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 0}];
let arr2 = [{name: 'Almendras', amount: 2}];
let expectedOutput = [{name: 'Almendras', amount: 2}, {name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 0}];
let concat = arr1.concat(arr2);
const output = Object.values(concat.reduce((x, y) => {
x[y.name] = x[y.name] && x[y.name].amount > y.amount ? x[y.amount] : y
return x
}, {}));
console.log(output);