【发布时间】:2014-10-08 08:22:00
【问题描述】:
我有两个数组:
var allObjects = [
{
ani: "082817093649",
customerID: "C20110324223733_971",
invoiceDate: "2014-05-20",
invoiceDebt: "0",
totalAmount: "160434",
totalUsage: "140849"
},
{
ani: "082817093649",
customerID: "C20110324223733_971",
invoiceDate: "2014-05-20",
invoiceDebt: "28631",
totalAmount: "28631",
totalUsage: "21028"
},
{
ani: "082817054972",
customerID: "C20111213222859_852",
invoiceDate: "2012-11-20",
invoiceDebt: "0",
totalAmount: "60500",
totalUsage: "14648"
},
{
ani: "082817054972",
customerID: "C20111213222859_852",
invoiceDate: "2014-02-20",
invoiceDebt: "0",
totalAmount: "60500",
totalUsage: "47986"
},
];
var objRow = [
{
customerID : "C20110324223733_971",
2014-05-20 : [0, 0, 0] // totalAmount, totalUsage, invoiceDebt
},
{
customerID : "C20111213222859_852",
2012-11-25 : [0, 0, 0] // totalAmount, totalUsage, invoiceDebt
},
{
customerID : "C20111213222859_852",
2014-02-20 : [0, 0, 0] // totalAmount, totalUsage, invoiceDebt
}
];
我想更新objRow 数组中的数据,其索引与allObjects 数组中的索引相同,该条件来自“customerID”值
如果objRow数组有相同的索引(日期索引),则值为allObjects数组的数据之和,并更新为objRow数组。
所以最终的结果是这样的:
var objRow = [
{
customerID : "C20110324223733_971",
2014-05-20 : [189065, 161877, 28631] // totalAmount, totalUsage, invoiceDebt
},
{
customerID : "C20111213222859_852",
2012-11-25 : [0, 0, 0] // totalAmount, totalUsage, invoiceDebt
},
{
customerID : "C20111213222859_852",
2014-02-20 : [60500, 47986, 0] // totalAmount, totalUsage, invoiceDebt
}
];
我已经尝试使用循环比较两个数组,但它在日期索引中重复。 :( 这是我的代码:
for (var b = 0; b < objRow.length; b++) {
for (var a = 0; a < allObjects.length; a++) {
if (objRow[b].customerID == allObjects[a].customerID) {
objRow[b][allObjects[a].invoiceDate] = [allObjects[a].totalAmount,allObjects[a].totalUsage,allObjects[a].invoiceDebt];
}
}
}
请帮忙,谢谢。
【问题讨论】:
-
最终结果中的第2个元素(index=1)是如何得到的?
-
哦,很抱歉
objRow数组中的那个元素。我编辑了我的问题。
标签: javascript arrays for-loop compare