比较两个对象数组并修改原始对象数组



让我们有两个对象数组作为,

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,
}
];

比较两个对象数组,如果价格存在于特定项目的newArrayOfObject中,则将价格插入该特定项目的oldArrayOfObject中,并设置hasPrice:true。

预期O/p:

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,
},
];

为此,我尝试了

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 };
}
});

但我无法从这里继续前进。如果有人需要进一步的解释或进一步的许可,请告诉我。

代码的问题是

  • 您必须在newArrayOfObject中找到来自oldArrayOfObject的节点。您的oldInfo应定义为oldInfo = newArrayOfObject.find((item) => item.Item === node.Item);
  • 此外,如果找到了oldInfo,则应将Price重新命名为Price: oldInfo.Price
  • 如果未找到oldInfo,则必须返回当前节点本身。不要在其中手动设置hasPrice: true

工作Fiddle

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总是从现有阵列创建一个新阵列。

如果要更新原始阵列本身而不是创建新阵列,可以在oldArrayOfObject上运行一个循环,并检查oldArrayOfObject中的每个节点是否都在newArrayOfObject中。如果找到匹配节点,则更新hasPricePrice

工作Fiddle

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);

您可以尝试在示例中使用此函数:

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")

如果您计划修改oldArrayOfObject,那么我不建议使用map。相反,使用一个简单的for循环-

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)

我认为您混淆了newArrayOfObjectsoldArrayOfObjects。这是您的解决方案:

const modifiedArrayOfObject = oldArrayOfObject.map((node) => {
const newInfo = newArrayOfObject.find((item) => item.Item === node.Item);
if (newInfo) return { ...node, hasPrice: true, Price: newInfo.Price };
return node;
});

我不建议您更改任何数组,让它们保持不变,并创建一个统一它们的新数组。然而,你走在了正确的轨道上,因为你最旧的列表有更多的项目,你应该浏览它,而不是新的,如果这种情况发生了变化,你的代码就会崩溃。

const modifiedArrayOfObject = oldArrayOfObject.map((oldInfo) => {
const newInfo = newArrayOfObject.find((newInfo) =>  newInfo.Item === oldInfo.Item);
if (newInfo && newInfo.Price) {
return { ...oldInfo, Price: newInfo.Price, hasPrice: true };
}
return { ...oldInfo, hasPrice: false };
});

console.log("modifiedArrayOfObject:", modifiedArrayOfObject);

另外,不要在没有回报的情况下使用地图。

相关内容

  • 没有找到相关文章

最新更新