function compareArrays(arr1, arr2) {
const result = arr1.map(item => {
const foundItem = arr2.find(fItem => fItem.key === item.key);
if (foundItem) {
return foundItem;
}
return item;
});
return result;
}
const arr1 = [{key: 'foo', value: 'bar'}, {key: 'hello', value: 'world'}];
const arr2 = [{key: 'foo', value: 'baz'}, {key: 'test', value: 'value'}];
console.log(compareArrays(arr1, arr2));
// output: [{key: 'foo', value: 'baz'}, {key: 'hello', value: 'world'}, {key: 'test', value: 'value'}]