Tengamos dos arreglos de objetos como,
let oldArrayOfObject = [ { Item: "ACC", Price: "", hasPrice: false, }, { Item: "BCC", Price: "", hasPrice: false, }, { Item: "CCC", Price: "", hasPrice: false, }, { Item: "DCC", Price: "", hasPrice: false, }, { Item: "ECC", Price: "", hasPrice: false, }, ]; let newArrayOfObject = [ { Item: "ACC", Price: 12, }, { Item: "BCC", Price: 50, }, { Item: "ECC", Price: 21, } ];Compare dos matrices de objetos y, si el precio existe en newArrayOfObject para un artículo en particular, inserte el precio en oldArrayOfObject para ese artículo en particular y establezca hasPrice: true.
O/P esperado:
console.log(oldArrayOfObject) [ { Item: "ACC", Price: 12, hasPrice: true, }, { Item: "BCC", Price: 50, hasPrice: true, }, { Item: "CCC", Price: "", hasPrice: false, }, { Item: "DCC", Price: "", hasPrice: false, }, { Item: "ECC", Price: 21, hasPrice: true, }, ];Para esto lo intenté como,
const modifiedArrayOfObject = newArrayOfObject.map((node) => { const oldInfo = oldArrayOfObject.find((item) => item.Item === node.Item); if (oldInfo) { return { ...node, hasPrice: oldInfo.Price !== node.Price } } else { return { ...node, hasPrice: true }; } });Pero soy incapaz de avanzar desde aquí. Por favor, hágamelo saber si alguien necesita más explicaciones o más autorización.
El problema con su código fue
oldArrayOfObject en newArrayOfObject . Su oldInfo debe definirse como oldInfo = newArrayOfObject.find((item) => item.Item === node.Item);oldInfo , debe devolver el Price como Price: oldInfo.PriceoldInfo se encuentre la información anterior, debe devolver el nodo actual. No establezca hasPrice: true manualmente en eso.violín de trabajo
const oldArrayOfObject = [{ Item: "ACC", Price: "", hasPrice: false, }, { Item: "BCC", Price: "", hasPrice: false, }, { Item: "CCC", Price: "", hasPrice: false, }, { Item: "DCC", Price: "", hasPrice: false, }, { Item: "ECC", Price: "", hasPrice: false, }]; const newArrayOfObject = [{ Item: "ACC", Price: 12, }, { Item: "BCC", Price: 50, }, { Item: "ECC", Price: 21 }]; const modifiedArrayOfObject = oldArrayOfObject.map((node) => { const oldInfo = newArrayOfObject.find((item) => item.Item === node.Item); if (oldInfo) { return { ...node, Price: oldInfo.Price, hasPrice: oldInfo.Price !== node.Price } } else { return { ...node }; } }); console.log(modifiedArrayOfObject); Array.map siempre crea una nueva matriz a partir de una matriz existente.
Si desea actualizar la matriz original en lugar de crear una nueva matriz, puede ejecutar un bucle en oldArrayOfObject y verificar si cada nodo de oldArrayOfObject está allí en newArrayOfObject . Luego actualice hasPrice y Price si se encuentra el nodo coincidente.
violín de trabajo
const oldArrayOfObject = [{ Item: "ACC", Price: "", hasPrice: false, }, { Item: "BCC", Price: "", hasPrice: false, }, { Item: "CCC", Price: "", hasPrice: false, }, { Item: "DCC", Price: "", hasPrice: false, }, { Item: "ECC", Price: "", hasPrice: false, }]; const newArrayOfObject = [{ Item: "ACC", Price: 12, }, { Item: "BCC", Price: 50, }, { Item: "ECC", Price: 21 }]; oldArrayOfObject.forEach((node) => { const oldInfo = newArrayOfObject.find((item) => item.Item === node.Item); if (oldInfo) { node.hasPrice = oldInfo.Price !== node.Price; node.Price = oldInfo.Price; } }); console.log(oldArrayOfObject);Puedes intentar usar esta función en el ejemplo:
let oldArrayOfObject = [ { Item: "ACC", Price: "", hasPrice: false, }, { Item: "BCC", Price: "", hasPrice: false, }, { Item: "CCC", Price: "", hasPrice: false, }, { Item: "DCC", Price: "", hasPrice: false, }, { Item: "ECC", Price: "", hasPrice: false, }, ]; let newArrayOfObject = [ { Item: "ACC", Price: 12, }, { Item: "BCC", Price: 50, }, { Item: "ECC", Price: 21, } ]; function modifiedArrayOfObject(item) { newArrayOfObject.map(v => { if(v.Item === item){ const index = oldArrayOfObject.findIndex(obj => obj.Item === v.Item); oldArrayOfObject[index] = { Item: v.Item, Price: v.Price, hasPrice: true }; } }) console.log("oldArrayOfObject:", oldArrayOfObject) } modifiedArrayOfObject("ACC")Si planea modificar oldArrayOfObject , entonces no sugeriría usar el map para esto. En su lugar, use un bucle for simple:
const oldArrayOfObject = ... const newArrayOfObject = ... for (const t of newArrayOfObject) { const q = oldArrayOfObject.find(o => o.Item=== t.Item) if (q == null) continue q.Price = t.Price q.hasPrice = true } console.log(oldArrayofObject)