[英]Recursively sum children property in an object
Hi I have the following object: 嗨,我有以下对象:
const ob =
{
"children": [
{
"children": [],
"code": "123",
"estimateTime": 20,
"groupId": 20,
"id": 2044,
"name": "1.1. CON ABC",
"order": 2,
"parentId": 2043
},
{
"children": [
{
"children": [],
"code": "123",
"estimateTime": 20,
"groupId": 20,
"id": 2044,
"name": "1.1. CON ABC",
"order": 2,
"parentId": 2043
}
],
"code": "123",
"estimateTime": 20,
"groupId": 20,
"id": 2044,
"name": "1.1. CON ABC",
"order": 2,
"parentId": 2043
},
],
"code": "CONG VIEC 1",
"estimateTime": 50,
"groupId": 20,
"id": 2043,
"name": "1. abc",
"order": 1,
"parentId": null
};
the children property is itself an array containing the same objects as their parent. children属性本身是一个包含与其父对象相同对象的数组。
How would I recursively sum all the estimateTime value inside the children and display it into the parent's estimateTime? 我将如何递归求和子级中所有的EstimateTime值并将其显示在父级的EstimateTime中?
I tried the following but it not sum all the values of estimateTime: 我尝试了以下方法,但未将sumstimateTime的所有值求和:
let estimateTime = ob.estimateTime;
function sumRecur(obj){
if(obj.hasOwnProperty("children")){
for(var i = 0;i<obj["children"].length;i++){
estimateTime += obj.children[i].estimateTime
}
return estimateTime;
}
return sumRecur(obj.children);
}
It returns 90 instead of 110. 它返回90而不是110。
I would really appreciate any help 我真的很感谢任何帮助
You're trying to do too much work here. 您正在尝试在这里做太多的工作。 Think of it this way: the total estimate for an element is its estimated time plus the sum of the total estimatess for each of its children. 这样想:元素的总估算值是元素的估算时间加上每个子元素的估算总值之和。 That converts almost directly to code. 几乎可以直接将其转换为代码。
const sum = (nbrs) => nbrs .reduce ((a, b) => a + b, 0) const totalEstimate = (ob) => ob .estimateTime + sum (ob .children .map (totalEstimate) ) const ob = {"children": [{"children": [], "code": "123", "estimateTime": 20, "groupId": 20, "id": 2044, "name": "1.1. CON ABC", "order": 2, "parentId": 2043}, {"children": [{"children": [], "code": "123", "estimateTime": 20, "groupId": 20, "id": 2044, "name": "1.1. CON ABC", "order": 2, "parentId": 2043}], "code": "123", "estimateTime": 20, "groupId": 20, "id": 2044, "name": "1.1. CON ABC", "order": 2, "parentId": 2043}], "code": "CONG VIEC 1", "estimateTime": 50, "groupId": 20, "id": 2043, "name": "1. abc", "order": 1, "parentId": null}; console .log ( totalEstimate (ob) )
I think the extraction of the sum
helper function makes this more readable, and sum
is clearly a useful function on its own. 我认为对sum
辅助函数的提取使此函数更具可读性,并且sum
本身显然是有用的函数。 But if you prefer, you could inline it: 但是,如果您愿意,可以内联它:
const totalEstimate = (ob) =>
ob .estimateTime +
ob .children .map (totalEstimate) .reduce ((a, b) => a + b, 0)
This question results in one of those pleasant recursions where the base case is handled automatically -- because sum
on an empty array returns 0. But if instead of containing an empty array as a children
property, an element with no children simply had no children
property, then the change would be relatively simple: 这个问题会导致那些令人愉快的递归中的一种自动处理基本情况的原因-因为空数组上的sum
返回0。但是,如果不将空数组作为children
属性而不是包含空数组,则没有子元素的元素就不会具有children
属性,那么更改将相对简单:
const totalEstimate = (ob) =>
ob .estimateTime + (ob .children ? sum (ob .children .map (totalEstimate) ) : 0)
Note also how easily this can now be abstracted to total on an arbitrary property: 还请注意,现在可以轻松地将其抽象为任意属性的总数:
const sum = (nbrs) => nbrs .reduce ((a, b) => a + b, 0) const totalProp = (prop) => (ob) => ob [prop] + sum (ob .children .map (totalProp (prop) )) const ob = {"children": [{"children": [], "code": "123", "estimateTime": 20, "groupId": 20, "id": 2044, "name": "1.1. CON ABC", "order": 2, "parentId": 2043}, {"children": [{"children": [], "code": "123", "estimateTime": 20, "groupId": 20, "id": 2044, "name": "1.1. CON ABC", "order": 2, "parentId": 2043}], "code": "123", "estimateTime": 20, "groupId": 20, "id": 2044, "name": "1.1. CON ABC", "order": 2, "parentId": 2043}], "code": "CONG VIEC 1", "estimateTime": 50, "groupId": 20, "id": 2043, "name": "1. abc", "order": 1, "parentId": null}; console .log ( totalProp ('estimateTime') (ob) )
This function can now be used to sum a recursive tree on any named property. 现在可以使用此函数对任何命名属性求和递归树。 And there are other abstractions that can easily be carried out here. 还有其他一些抽象可以在这里轻松实现。 (What if it were "subitems"
rather than "children"
, for instance?) (如果它是"subitems"
,而不是"children"
,比如?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.