Pregunta de entrevista de Walmart

merge two objects in javascript

Respuesta de la entrevista

Anónimo

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)