backbone.js - How to print Count, Sum of backbone collections items using underscore.js and -


i have following

 var cart= [{ item: "a", price: 2 },              { item: "b", price: 3 },              { item: "a", price: 2 },              { item: "c", price: 5 },              { item: "c", price: 5 },              { item: "a", price: 2 }]; 

now wanted output like.

item   price   qty   total      2       3     6     b      3       1     3 c      5       2     10 

for given item:

var sub = mycollection.where({item: item}),     length = sub.length,     total = _.reduce(sub, function(memo, num){ return memo + num; }, 0); 

if want items can take unlimited number of values, recommend sort collection first, loop on found values.


Comments

Popular posts from this blog

php - Passing multiple values in a url using checkbox -

compilation - PHP install fails on Ubuntu 14 (make: *** [sapi/cli/php] Error 1) PHP 5.6.20 -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -