I have two arrays of objects and want to combine the two based on specific key-value pairs. For example, I want to merge each of the objects in the arrays if they have the same values for 'fruit' key:
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 = [
{fruit: 'banana', price:100, quality:'high', count:4},
{fruit: 'orange', price:50, quality:'average', count:10 }
];
The arrays a and b are defined such that they have the same length.
Thought about creating new empty arrays (e.g. 'banana' and 'orange') and then pushing relevant elements from each of the defined arrays, but this is probably overkilled and would love it if someone can help me out by showing a simple way to do this.
Thanks!