javascript - Underscore summing values in key value pairs -


i have following object:

{key: [{thing: value, stuff: value}, {thing: value, stuff: value}], key2: [{thing: value, stuff: value}, {thing: value, stuff: value}]} 

for each key, i'd either array of stuff values each key or better yet sum of stuff values each key. stuff values numbers want sum. i've been @ day , @ wits end.

here exact form of data: top level: [array[1], array[1], array[3], array[1], array[1], array[1], array[2], array[1], array[1], array[1], array[1], array[1], array[1], array[1], array[1]]

here array[3]:

2: array[3] 

the first object in array[3]:

0: object

the contents of object:

amounts: array[3] avg: 1202.38 bill: 871.43 

there dozen key value pairs in each object. want sum of "bill" each array grouped user already. bill exists within each object within each array.

to version see @ top did:

_.groupby(_.flatten(bigarray),'name'); 

i thought move in right direction makes explicit user since need total bill each name.

is after?

var d = {    key: [{      thing: "a",      stuff: 1    }, {      thing: "b",      stuff: 2    }],    key2: [{      thing: "a",      stuff: 3    }, {      thing: "b",      stuff: 4    }]  };  var result = _.map(d, function(arr, key) {    var stuffvalues = _.pluck(arr, 'stuff');    return {      key: key,      stuffsum: _.reduce(stuffvalues, function(memo, num) {        return memo + +num;      }, 0)    };  });  document.getelementbyid('output').textcontent = json.stringify(result);  // => [{"key":"key","stuffsum":3},{"key":"key2","stuffsum":7}]
<script src="//underscorejs.org/underscore-min.js"></script>  <pre id="output"></pre>


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -