gpt4 book ai didi

javascript - 如果其他参数匹配,则对数组中的参数求和

转载 作者:行者123 更新时间:2023-11-27 23:35:41 26 4
gpt4 key购买 nike

我有一个如下的数组

如果资源 ID 和开始日期与其他条目匹配,我想对 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"}
]}];

在上面的示例中,ResourceId“abc”,开始日期为“2015-08-13 00:00”,有 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"}
]}]

最佳答案

用太循环来做。首先创建一个带有 '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"]];
})
}
})

关于javascript - 如果其他参数匹配,则对数组中的参数求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34157040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com