我有一个数组如下
如果资源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"}
]}];
在上面的示例中,开始日期为“ 2015-08-13 00:00”的ResourceId“ abc”具有3个条目,其TotalHrs值为4,4,2,总计为10。
我想将这3个条目的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"}
]}]
最佳答案
此解决方案适用于临时对象,用于分组项的总和:
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>');