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

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

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

asp.net mvc - breakpoint on javascript in CSHTML? -