我有一个带有项目的集合:
Object_id: "CoQayo8QmhEJduwtr"
category: "Computers"
createdAt: Sat May 23 2015 17:53:30 GMT-0400 (EDT)link: "https://www.apple.com/mac-pro/"
price: "4999.00"
title: "Mac Pro"
updatedAt: Sun May 24 2015 01:27:06 GMT-0400 (EDT)
userId: "yN36TFg742wZcyjQG"
我有一个页面上显示的每个用户的项目列表。如何显示当前用户的价格字段总和?
答案 0 :(得分:1)
您可以使用模板助手以及多个下划线功能的组合:
Template.user.helpers({
// declare a new helper on your user page template
priceSum: function(){
// fetch every items belonging to the currently displayed user
var userItems = Items.find({
userId: this._id
}).fetch();
// extract the price property in an array
var userItemsPrices = _.pluck(userItems, "price");
// compute the sum using a simple array reduction
return _.reduce(userItemsPrices, function(sum, price){
return sum + parseFloat(price);
}, 0);
}
});