比较两个数组并更新Javascript中的第二个数组元素

比较两个数组并更新Javascript中的第二个数组元素,javascript,arrays,for-loop,compare,Javascript,Arrays,For Loop,Compare,我有两个阵列: var allObjects = [ { ani: "082817093649", customerID: "C20110324223733_971", invoiceDate: "2014-05-20", invoiceDebt: "0", totalAmount: "160434", totalUsage: "140849" }, { ani

我有两个阵列:

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
    }
];
我想用
allObjects
数组中相同的索引更新
objRow
数组中的数据,该条件来自“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
    }
];
我已经尝试使用loop比较两个数组,但它会在日期索引中重复:( 这是我的密码:

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

        }

    }

}
for(var b=0;b

请帮助,谢谢。

首先,您必须将临时对象
映射
customerID
invoiceDate
填充到适当的属性值。然后您可以使用它来获得所需的结果:

var map = {};
for (var n = allObjects.length, i = 0; i < n; i++) {
    var el = allObjects[i], id = el.customerID, date = el.invoiceDate;
    if (!(id in map)) map[id] = {};
    if (!(date in map[id])) map[id][date] = [0, 0, 0];
    var arr = map[id][date];
    arr[0] += el.totalAmount;
    arr[1] += el.totalUsage;
    arr[2] += el.invoiceDebt;
}

n = objRow.length; i = 0;
for (; i < n; i++) {
    var el = objRow[i], id = el.customerID;
    for (var key in el) {
        if (key != 'customerID')
            el[key] = id in map && key in map[id]? map[id][key] : [0,0,0];
    }
}
var-map={};
for(var n=allObjects.length,i=0;i
最终结果中的第二个元素(索引=1)是如何获得的?哦,我很抱歉
objRow
数组中的那个元素。我编辑了我的问题。谢谢,但是如何用[0,0,0]值替换
未定义的