Frage im Vorstellungsgespräch bei Walmart

merge two objects in javascript

Antwort im Vorstellungsgespräch

Anonym

1. Feb. 2021

function mergeTwo(original, update) { return original.reduce((acc, el, idx) => { if (el.id === update[idx].id) { acc.push({...el, ...update[idx]}) } else if (el.id !== update[idx].id) { acc.push( {...el}, {...update[idx]} ) } return acc }, []) } mergeTwo(obj.object1, obj.object2)