underscore.js is handling the templating on the page, and I have jQuery on my page also.
I have an array that looks something like this:
orders = [{q:5},{q:5},{q:5},{q:5},{q:5}];
Now on the page I want the sum of q printed, but I cannot get it to work.
I tried a lot of code snippets, but none of them seem to work. The one I thought had do work is this one:
<td><% print( _.reduce(orders.q), function(memo, num){ return memo + num; }, 0 ) %></td>
It’s from the page of underscore itself.
This piece of code gives me:
undefined is not a function;
orders.qlooks for a propertyqon the arrayorders— soorders.qis most likely returningundefined, which is indeed not a function.You probably want to pass
_.pluck(orders, 'q'), which returns an array of the values ofqfor each item inorders, to_.reduce. And double-check your parentheses: