我有一个收藏品:
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"
我在页面上为每个用户显示了一个项目列表。如何显示当前用户的价格字段总和?
您可以将模板帮助程序与几个下划线功能结合使用:
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);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句