score:1
Accepted answer
you can use a recursive function:
const obj = {
"name": "a1",
"children": [{
"name": "b1",
"children": [{
"name": "b1-c1",
"children": [{
"name": "b1-c1-d1",
"children": [{
"name": "b1-c1-d1-e1",
"value": 30,
"score": 0.8
},
{
"name": "b1-c1-d1-e2",
"value": 35,
"score": 0.5
}
]
},
{
"name": "b1-c1-d2",
"children": [{
"name": "b1-c1-d2-e1",
"value": 31,
"score": 0.4
},
{
"name": "b1-c1-d2-e2",
"value": 23,
"score": 0.7
}
]
}
]
}]
}]
}
function getwithaveragescore(objtorecurse) {
// if i have a score i am already done
if (objtorecurse.score) {
return objtorecurse;
}
// otherwise, i get my children with their average score
const children = objtorecurse.children.map(getwithaveragescore);
return {
...objtorecurse,
children,
// and i set my scoreavg to their average (score or scoreavg)
scoreavg: children.reduce((total, { score, scoreavg }) => total + (score || scoreavg), 0) / children.length
};
}
console.log(json.stringify(getwithaveragescore(obj), null, 2))
score:1
let o = {
"name": "a1",
"children": [
{
"name": "b1",
"children": [
{
"name": "b1-c1",
"children": [
{
"name": "b1-c1-d1",
"children": [
{
"name": "b1-c1-d1-e1",
"value": 30,
"score": 0.8
},
{
"name": "b1-c1-d1-e2",
"value": 35,
"score": 0.5
}
]
},
{
"name": "b1-c1-d2",
"children": [
{
"name": "b1-c1-d2-e1",
"value": 31,
"score": 0.4
},
{
"name": "b1-c1-d2-e2",
"value": 23,
"score": 0.7
}
]
}
]
}
]
}
]
};
function avgup(object){
object.avgscore = 0;
if(object.children){
for(child of object.children){
object.avgscore += avgup(child);
}
object.avgscore = object.avgscore /math.max(1,object.children.length);
return object.avgscore;
}else{
return object.score;
}
}
avgup(o);
console.log(json.stringify(o));
Source: stackoverflow.com
Related Query
- d3 hierarchy ability to average children node values
- How do I remove all children elements from a node and then apply them again with different color and size?
- d3.nest() key and values conversion to name and children
- Node size proportional to number of children in D3
- d3.js moving average with previous and next data values
- Removing node and its children from d3js tree layout
- DC.js Reductio - Display cumulative average of values within date range DC
- selection.join behavior when each node has children
- Root node size to take sum of children nodes size when collapsed
- Select the children of a node from a tree in d3.js
- Understanding D3 Force-Layout: Why previous node values x,y NOT EQUAL px,py after 1 timestep?
- Drawing a radial tree from node and edges rather than children
- use a gradient for a d3 diagonal based on node values
- json getting children's children nodes hierarchy
- Draw gridlines at the average (or median) values of a range in d3.js
- How do I loop through an inner array of values in d3 and add corresponding children elements?
- D3 Tree: How to make children checked and disabled recursively when the parent node is checked
- d3js v4 Create filter function from node values that include arrays
- D3 v4 drag/drop tree node into new parents children
- Manipulating node parent children behaviour on the basis of checkbox
- How to increase the width of a particular node of a hierarchy in D3.js?
- Want to transform position all nodes after a specific node in a d3 tree structure hierarchy
- Mouse event handling in a node and its children
- Checking parents with children of a node
- Fix Node Position in D3 Force Directed Layout
- Highlight selected node, its links, and its children in a D3 force directed graph
- d3 Node Labeling
- How to get data of parent node in d3.js
- How can I make double click event on node in d3.js?
- d3 selector for immediate children
More Query from same tag
- Issue formatting data in JavaScript for a pie chart
- Is there any possibility of using imported fonts with r2d3?
- D3.js show heatmap for each pixel (coordinate) in choropleth
- Loop through paths D3 with .each
- D3 Line chart using category name (string) in x-axis
- How to adjust svg height within table cell?
- d3 + create pie chart with different formats
- Convert flat JSON file to hierarchical json data like flare.json [d3 example file]
- How to use brush to select circles that are contained in multiple group elements in D3.js?
- D3 datamaps datastructure (multiple years, all countries)
- d3.js bar matrix - reset height if different column
- Draw line chart using dc js
- D3.js implement ellipsis for axis labels SVG
- How to move connecting path/lines in D3 to edges of shape
- Can i give spaces between the bar rectangles without adjusting the height attribute?
- Multiple force-layout graphs with d3 in seperate svg/div's
- how would I access this selector using child/parent notation
- D3: Drawing 3 csv in 1 graph with a function for each, but 2 don't work
- Neat solution to convert flat data to nested JSON
- Morphing SVG line from straight to expanded
- How do I tell d3 to not repeat values in ticks?
- axis text rotation inside group in d3 bar chart
- Graph orientation and node positioning in d3.js
- D3js Line graph doesn't go through all of my points when I use .interpolate('basis')?
- Adding a label to a path with D3
- Html/D3 tooltip position issues
- What ever happend to the d3j.s force directed graph cluster example see on this image?
- Resize D3 ForceSimulation on d3.vs4
- d3 js bar mouseover
- D3.js code not working for Creating circles based on data