I have two Arrays :
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
}
];
I want to update the data in objRow array with index that same in allObjects array, which condition is from 'customerID' value
If the objRow array has same index (date index), the value is Sum of data from allObjects array, and update to objRow array.
So the final result like this :
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
}
];
I already try to compare two arrays using loop, But it makes duplicate in date index. :( Here is my code :
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];
}
}
}
Please help, thank you.
objRowarray. I edited my question.