简体   繁体   English

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

[英]Compare two arrays and update second array element in Javascript

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. 我想更新在数据objRow具有索引,在同一阵列allObjects阵列,其条件是从“的customerID”值。如果objRow阵列具有相同的索引(日期索引),该值是数据的总和从allObjects阵列,并更新到objRow数组。

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. 请帮忙,谢谢。

First you have to populate temporary object map mapping customerID and invoiceDate to appropriate property values. 首先,你必须填写临时对象map测绘customerIDinvoiceDate到合适的属性值。 Then you can use it to obtain the desired result: 然后,您可以使用它来获得所需的结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript 比较两个多维 arrays 的变化,然后用这些变化更新第二个数组 - javascript compare two multidimensional arrays for changes then update the second array with those changes Javascript比较两个不同大小的数组,并返回不在第二个数组中的项目 - Javascript Compare two arrays with different sizes and return the items that are not in second array 比较两个 arrays ,其中显示数组中的元素是否更大 Javascript - Compare two arrays where show if an element is greater in an array Javascript 推送到数组并比较两个数组的JavaScript - Push to array and compare two arrays JavaScript 数组和JavaScript-如何比较两个数组 - Array and javascript - how to compare two arrays 比较两个 arrays 并将相同的元素推送到一个数组中,将 rest 推送到另一个数组中的 javascript - Compare two arrays and push same element in one array and rest in other array in javascript 比较两个数组中的元素 - compare element in two arrays 比较JavaScript中的两个数组 - Compare two arrays in javascript 比较第二个数组中不存在的两个对象数组和过滤器元素 - Compare two array of objects and filter element not present in second array Javascript:按每个内部数组中的第二个元素对数组排序 - Javascript: Sort array of arrays by second element in each inner array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM