如果其他参数匹配,则从数组中汇总参数

时间:2015-12-08 13:23:04

标签: javascript jquery arrays

我有一个数组如下

如果资源ID和startdate与其他条目匹配,我想总结TotalHrs参数。

var arr = [
    {"Id":123,
        "Subevents":[
            {"Id":225,"ParentId":123,"ResourceId":"abc","Name":"4.00","TotalHrs":"4.00","StartDate":"2015-08-13 00:00","EndDate":"2015-08-13 23:59"},
            {"Id":226,"ParentId":123,"ResourceId":"abc","Name":"4.00","TotalHrs":"4.00","StartDate":"2015-08-13 00:00","EndDate":"2015-08-13 23:59"},
            {"Id":227,"ParentId":123,"ResourceId":"xyz","Name":"8.00","TotalHrs":"8.00","StartDate":"2015-08-14 00:00","EndDate":"2015-08-14 23:59"},
            {"Id":228,"ParentId":123,"ResourceId":"xyz","Name":"8.00","TotalHrs":"8.00","StartDate":"2015-08-14 00:00","EndDate":"2015-08-14 23:59"}
        ]
    },
    {"Id":335,
        "Subevents":[
            {"Id":345,"ParentId":335,"ResourceId":"abc","Name":"2.00","TotalHrs":"2.00","StartDate":"2015-08-13 00:00","EndDate":"2015-08-13 23:59"},
            {"Id":346,"ParentId":335,"ResourceId":"pqr","Name":"6.00","TotalHrs":"6.00","StartDate":"2015-08-14 00:00","EndDate":"2015-08-14 23:59"}
        ]}];

在上面的示例中,使用startdate为“2015-08-13 00:00”的ResourceId“abc”有3个条目,其中TotalHrs值为4,4,2,总计为10。

我想将这三个条目的TotalHrs值更新为10.

同样,ResourceId =“xyz”和StartDate =“2015-08-14 00:00”有2个此类条目,其中TotalHrs值等于8,8等于16。

因此,如果任何其他条目与StartDate和ResourceId匹配,我的结果数组应仅更新TotalHrs值

因此我的最终数组应该如下

[
    {"Id":123,
        "Subevents":[
            {"Id":225,"ParentId":123,"ResourceId":"abc","Name":"4.00","TotalHrs":"10","StartDate":"2015-08-13 00:00","EndDate":"2015-08-13 23:59"},
            {"Id":226,"ParentId":123,"ResourceId":"abc","Name":"4.00","TotalHrs":"10","StartDate":"2015-08-13 00:00","EndDate":"2015-08-13 23:59"},
            {"Id":227,"ParentId":123,"ResourceId":"xyz","Name":"8.00","TotalHrs":"16","StartDate":"2015-08-14 00:00","EndDate":"2015-08-14 23:59"},
            {"Id":228,"ParentId":123,"ResourceId":"xyz","Name":"8.00","TotalHrs":"16","StartDate":"2015-08-14 00:00","EndDate":"2015-08-14 23:59"}
        ]
    },
    {"Id":335,
        "Subevents":[
            {"Id":345,"ParentId":335,"ResourceId":"abc","Name":"2.00","TotalHrs":"10","StartDate":"2015-08-13 00:00","EndDate":"2015-08-13 23:59"},
            {"Id":346,"ParentId":335,"ResourceId":"pqr","Name":"6.00","TotalHrs":"6.00","StartDate":"2015-08-14 00:00","EndDate":"2015-08-14 23:59"}
        ]}]

2 个答案:

答案 0 :(得分:1)

用太多循环来做。首先使用'ResourceId'=>创建一个对象'totalHrs',第二次循环遍历数组并根据计算出的值分配totalHrs。

var totals = {};
arr.forEach(function (element) {
    if (element["Subevents"]) {
        var arr2 = element["Subevents"];
        arr2.forEach(function (element) {
            totals[element["ResourceId"] + element["StartDate"]] = totals[element["ResourceId"] + element["StartDate"]] || 0;
            totals[element["ResourceId"] + element["StartDate"]] += parseInt(element["TotalHrs"]);
        })
    }
});

arr.forEach(function (element) {
    if (element["Subevents"]) {
        var arr2 = element["Subevents"];
        arr2.forEach(function (element) {
            element["TotalHrs"] = totals[element["ResourceId"] + element["StartDate"]];
        })
    }
})

答案 1 :(得分:0)

此解决方案适用于分组项目总和的临时对象:



var arr = [{ "Id": 123, "Subevents": [{ "Id": 225, "ParentId": 123, "ResourceId": "abc", "Name": "4.00", "TotalHrs": "4.00", "StartDate": "2015-08-13 00:00", "EndDate": "2015-08-13 23:59" }, { "Id": 226, "ParentId": 123, "ResourceId": "abc", "Name": "4.00", "TotalHrs": "4.00", "StartDate": "2015-08-13 00:00", "EndDate": "2015-08-13 23:59" }, { "Id": 227, "ParentId": 123, "ResourceId": "xyz", "Name": "8.00", "TotalHrs": "8.00", "StartDate": "2015-08-14 00:00", "EndDate": "2015-08-14 23:59" }, { "Id": 228, "ParentId": 123, "ResourceId": "xyz", "Name": "8.00", "TotalHrs": "8.00", "StartDate": "2015-08-14 00:00", "EndDate": "2015-08-14 23:59" }] }, { "Id": 335, "Subevents": [{ "Id": 345, "ParentId": 335, "ResourceId": "abc", "Name": "2.00", "TotalHrs": "2.00", "StartDate": "2015-08-13 00:00", "EndDate": "2015-08-13 23:59" }, { "Id": 346, "ParentId": 335, "ResourceId": "pqr", "Name": "6.00", "TotalHrs": "6.00", "StartDate": "2015-08-14 00:00", "EndDate": "2015-08-14 23:59" }] }],
    temp = {};

arr.forEach(function (a) {
    a.Subevents.forEach(function (b) {
        temp[b.ResourceId] = temp[b.ResourceId] || {};
        temp[b.ResourceId][b.StartDate] = (temp[b.ResourceId][b.StartDate] || 0) + +b.TotalHrs;
    });
});

arr.forEach(function (a) {
    a.Subevents.forEach(function (b) {
        b.TotalHrs = temp[b.ResourceId][b.StartDate];
    });
});
document.write('<pre>temp: ' + JSON.stringify(temp, 0, 4) + '</pre>');
document.write('<pre>arr: ' + JSON.stringify(arr, 0, 4) + '</pre>');
&#13;
&#13;
&#13;