Techy
Techy

Reputation: 41

Sum up a parameter from array if other parameter matches

I have an array as below

I want to sum up the TotalHrs Parameter if resource id and startdate matches with other entries.

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

In above example ResourceId "abc" with startdate as "2015-08-13 00:00" have 3 entries with TotalHrs value as 4,4,2 which sums to 10.

I want to update the TotalHrs value to 10 for these 3 entries.

Again , ResourceId = "xyz" and StartDate = "2015-08-14 00:00" have 2 such entries with TotalHrs value equal to 8,8 which is equal to 16.

Thus my resulting array should only update TotalHrs value if any other entries have a match with StartDate and ResourceId

Hence my final array should be like below

[
    {"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"}
        ]}]

Upvotes: 1

Views: 56

Answers (2)

Stepashka
Stepashka

Reputation: 2698

Do it with too loops. First create an object with 'ResourceId' => 'totalHrs', second time loop through the array and assign the totalHrs based on the calculated value.

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

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386604

This solution works with a temporary object for the sums of the grouped items:

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>');

Upvotes: 0

Related Questions