javascript - Can I make this function more concise -


i have javascript snippet , wondering if can calculate amount , users in single pass of reduce function?

root.children.foreach(function(v) {      v.amount = v.children.reduce(function(a, b) {         console.log(a);         return {             amount: a.amount + b.amount         }     }, {         'amount': 0     }).amount      v.users = v.children.reduce(function(a, b) {         console.log(a);         return {             users: a.users + b.users         }     }, {         'users': 0     }).users  }) 

you single array#foreach loop.

var root = {          children: [              { children: [                  { amount: 2, users: 3 },                  { amount: 7, users: 5 }              ]}          ]      };    root.children.foreach(function(v) {      v.amount = 0;      v.users = 0;        v.children.foreach(function(a) {          v.amount += a.amount;          v.users += a.users;      });  });  console.log(root);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -