English 中文(简体)
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 17 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
原标题:Parent value as sum of all children values within nested javascript object

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 14 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 12 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

// 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: []
                }]
            }]
        }]
    }
}
最佳回答

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;
}

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 11 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

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

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

问题回答

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 09 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

function calculateValues(o) {
    o.valueTotal = (o.children || []).reduce(function (r, a) {
        calculateValues(a);				
        return r + (a.value || 0) + (a.valueTotal || 0);
    }, 0);
}

var names = { name:  name-1 , value: 10, valueTotal: 0, children: [{ name:  name-1.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2 , value: 10, valueTotal: 0, children: [{ name:  name-1.2.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2.2 , value: 10, valueTotal: 0, children: [{ name:  name-1.2.2.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2.2.2 , value: 10, valueTotal: 0, children: [] }] }] }] };

calculateValues(names);
console.log(names);
.as-console-wrapper { max-height: 100% !important; top: 0; }

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 07 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

// const objectScan = require( object-scan );

const injectSums = (data) => {
  objectScan([ **.children ], {
    filterFn: ({ parent, value: children }) => {
      parent.valueTotal = children
        .map(({ valueTotal, value }) => valueTotal + value)
        .reduce((a, b) => a + b, 0);
    }
  })(data);
};

const names = { name:  name-1 , value: 10, valueTotal: 0, children: [ { name:  name-1.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2 , value: 10, valueTotal: 0, children: [ { name:  name-1.2.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2.2 , value: 10, valueTotal: 0, children: [ { name:  name-1.2.2.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2.2.2 , value: 10, valueTotal: 0, children: [] } ] } ] }] };

injectSums(names);

console.log(names);
// => { name:  name-1 , value: 10, valueTotal: 60, children: [ { name:  name-1.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2 , value: 10, valueTotal: 40, children: [ { name:  name-1.2.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2.2 , value: 10, valueTotal: 20, children: [ { name:  name-1.2.2.1 , value: 10, valueTotal: 0, children: [] }, { name:  name-1.2.2.2 , value: 10, valueTotal: 0, children: [] } ] } ] } ] }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 06 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 04 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

function sumUpTree(obj, key, totalKey, childrenKey) {
  
  let sumUpTreeInner = (obj) => {
    obj[totalKey] =
      obj[key] +
      (obj[childrenKey] || []).reduce(function (r, child) {
        sumUpTreeInner(child);
        return r + (child[totalKey] || 0);
      }, 0);
  };
  sumUpTreeInner(obj);
}

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 58 MINUTES 03 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签