Solution 1: In case of the order of data and/or the different lengthIn case of the order of data and/or the different length
You can do the trick to render count property by using Conditionally add properties to an Object like this
const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];
const result = a.map(({fruit, price, quality}) => {
const index = b.findIndex(r => r.fruit === fruit);
return {
fruit, price, quality,
...(index >= 0 && {count: b[index].count}) // the trick here
};
});
console.log(result);
Solution 2:, If the 2 arrays make sure that the same length and order,
If the 2 arrays make sure that the same length and order, you can do simply like this
const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];
const result = a.map((item, index) => Object.assign({}, item, b[index]));
//equivalent to: a.map((item, index) => ({...item, ...b[index]}));
console.log(result);