Parent value as sum of all children values within nested javascript object

Sam

I have a deeply nested javascript object with an unlimited amout of children. Every child has a value and a totalValue. The totalValue should be the sum of all the values from all its children and subchildren. How can I make this work?

At the moment I'm only able to loop the whole object using a recursive function:

// Recursive function
_.each(names, function(parent) { 
    if(parent.children.length > 0) { 
        recursiveFunction(parent.children);
    }
});

function recursiveFunction(children){ 
    _.each(children, function(child) { 
        if(child.children.length > 0) { 
            recursiveFunction(child.children)
        }
    });
}; 

// Deeply nested javascript object
var names = {
    name: 'name-1',
    value: 10,
    valueTotal: 0, // should be 60 (name-1.1 + name-1.2 + name-1.2.1 + name-1.2.2 + name-1.2.2.1 + name-1.2.2.2)
    children: [{
            name: 'name-1.1',
            value: 10,
            valueTotal: 0,
            children: []
        }, {
            name: 'name-1.2',
            value: 10,
            valueTotal: 0, // should be 40 (name-1.2.1 + name-1.2.2 + name-1.2.2.1 + name-1.2.2.2)
            children: [{
                name: 'name-1.2.1',
                value: 10,
                valueTotal: 0,
                children: []
            }, {
                name: 'name-1.2.2',
                value: 10,
                valueTotal: 0, // should be 20 (name-1.2.2.1 + name-1.2.2.2)
                children: [{
                    name: 'name-1.2.2.1',
                    value: 10,
                    valueTotal: 0,
                    children: []
                }, {
                    name: 'name-1.2.2.2',
                    value: 10,
                    valueTotal: 0,
                    children: []
                }]
            }]
        }]
    }
}
Jonas Wilms

So in fact you wanna do sth like this: every elem asks his childs for its values, these do the same and give back their totalValues plus their own value.

function sumUp(object){
  object.totalValue = 0;
  for(child of object.children){
    object.totalValue += sumUp(child);
   }
   return object.totalValue + object.value;
}

Start like this:

const totalofall = sumUp(names);
console.log(names); //your expected result.

Working example: http://jsbin.com/laxiveyoki/edit?console

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get a single property of all children in a nested object using javascript?

From Dev

Calculating the sum of a variable within all children (recursive)

From Dev

MySql: Getting sum of a table by the id of its parent from other table and return all children with sum values that are relevant to parent_id

From Dev

search value within all keys in a nested elasticsearch object

From Dev

create object parent which nested children in mongoose

From Dev

Sorting an array object by nested values within objects - Javascript

From Dev

Loop through nested json array object and sum values javascript

From Dev

Xpath getting sum of all children and grandchildren values

From Dev

How to sum values within nested foreach loops?

From Dev

Sharing parent property value with all derived children

From Dev

Nested functions within an object javascript

From Dev

Get a sum of all values that have matching keys in a JavaScript Object?

From Dev

Does parent object know its children in JavaScript?

From Dev

JavaScript: Change All Values of Deeply nested object given path

From Dev

Use recursion to get parent value and children value and all its children's children value

From Dev

Javascript Sum array of object values

From Dev

javascript getters and setters for all children in object

From Dev

javascript getters and setters for all children in object

From Dev

Get all parent in a nested object using recursion

From Dev

parent nodes value to be the sum of all its childrens

From Dev

Targeting all children of parent

From Dev

Getting Parent Object, from nested value

From Dev

Keep resizable children within parent

From Dev

xpath exclude element and all its children by parent attribute containing a value

From Dev

Javascript: Finding index of an object in an array, based on a value within a nested array on the objects

From Dev

Get value from key of parent and pass it to children key in object

From Dev

Insert sum of all parent branches in each branch of a nested tree structure

From Dev

Javascript: Convert nested object to object with values as path

From Dev

Assemble (templates): Get parent value from within nested each block

Related Related

  1. 1

    How to get a single property of all children in a nested object using javascript?

  2. 2

    Calculating the sum of a variable within all children (recursive)

  3. 3

    MySql: Getting sum of a table by the id of its parent from other table and return all children with sum values that are relevant to parent_id

  4. 4

    search value within all keys in a nested elasticsearch object

  5. 5

    create object parent which nested children in mongoose

  6. 6

    Sorting an array object by nested values within objects - Javascript

  7. 7

    Loop through nested json array object and sum values javascript

  8. 8

    Xpath getting sum of all children and grandchildren values

  9. 9

    How to sum values within nested foreach loops?

  10. 10

    Sharing parent property value with all derived children

  11. 11

    Nested functions within an object javascript

  12. 12

    Get a sum of all values that have matching keys in a JavaScript Object?

  13. 13

    Does parent object know its children in JavaScript?

  14. 14

    JavaScript: Change All Values of Deeply nested object given path

  15. 15

    Use recursion to get parent value and children value and all its children's children value

  16. 16

    Javascript Sum array of object values

  17. 17

    javascript getters and setters for all children in object

  18. 18

    javascript getters and setters for all children in object

  19. 19

    Get all parent in a nested object using recursion

  20. 20

    parent nodes value to be the sum of all its childrens

  21. 21

    Targeting all children of parent

  22. 22

    Getting Parent Object, from nested value

  23. 23

    Keep resizable children within parent

  24. 24

    xpath exclude element and all its children by parent attribute containing a value

  25. 25

    Javascript: Finding index of an object in an array, based on a value within a nested array on the objects

  26. 26

    Get value from key of parent and pass it to children key in object

  27. 27

    Insert sum of all parent branches in each branch of a nested tree structure

  28. 28

    Javascript: Convert nested object to object with values as path

  29. 29

    Assemble (templates): Get parent value from within nested each block

HotTag

Archive