【发布时间】:2018-05-09 08:44:26
【问题描述】:
当产品 Line.Id 与退款 ID 匹配时,它应该从退款变量中删除该对象。
refund 变量应该只剩下 1 行,即:
refunds = {
Lines: [
{LineId: "444"}
]
}
我做错了什么?
查看示例:https://jsfiddle.net/ua8f1a5x/8/
products = {
Items: [{
Name: "Item Name 1",
LineId: "111",
Status: [],
},
{
Name: "Item Name 2",
LineId: "222",
Status: [],
},
{
Name: "Item Name 3",
LineId: "333",
Status: [],
}
]
}
refunds = {
Lines: [
{LineId: "222"},
{LineId: "111"},
{LineId: "444"}
]
}
refunds.Lines.forEach((refundItem, refundIndex) => {
console.log("Checking Id " + refundItem.LineId);
products.Items.forEach((Item) => {
if (refundItem.LineId == Item.LineId) {
Item.Status.push({Name: "Refunded"});
//Delete object from refund
refunds.Lines.splice(refundIndex, 1);
}
});
});
console.log(refunds);
console.log(products);
【问题讨论】:
-
在迭代数组时修改/删除数组中的项不是一个好主意
-
@GeorgeBailey 回答解决方案。
标签: javascript node.js