如何访问 Meteor 数组中嵌套对象中的对象
How can I access Object in an Nested Object in an Array in Meteor
我正在使用以下出版物来汇总我的产品的有效试用:
Meteor.publish('pruebasActivas', function() {
var pruebasActivas = Clientes.aggregate([
{
$match: {
'saldoPrueba': {
'$gt': 0
}
}
}, {
$group: {
_id: {
id: '$_id',
cliente: '$cliente'
},
totalPruebas: {
$sum: '$saldoPrueba'
}
}
}
]);
});
if (pruebasActivas && pruebasActivas.length > 0 && pruebasActivas[0]) {
return this.added('aggregate3', 'dashboard.pruebasActivas', pruebasActivas);
}
结果抛出以下对象
{
"0": {
"_id": {
"id": "YByiuMoJ3shBfTyYQ",
"cliente": "Foo"
},
"totalPruebas": 30000
},
"1": {
"_id": {
"id": "6AHsPAHZhbP3fCBBE",
"cliente": "Foo 2"
},
"totalPruebas": 20000
},
"_id": "dashboard.pruebasActivas"
}
我如何使用 Blaze 遍历包含对象的数组以显示 "cliente" 和 "totalPruebas"?
让自己成为一个将对象转换为对象数组的助手,仅使用未命名的顶级键_id
:
Template.myTemplate.helpers({
pruebasActivas: function(){
var ob = myCollection.findOne(); // assuming your collection returns a single object
var clientes = [];
for (var p in ob){
if (ob.hasOwnProperty(p) && p !== "_id"){
// here we flatten the object down to two keys
clientes.push({cliente: ob[p]._id.cliente, totalPruebas: ob[p].totalPruebas});
}
}
return clientes;
}
});
现在你可以做:
<template name="myTemplate">
{{#each pruebasActivas}}
Cliente: {{cliente}}
Total Pruebas: {{totalPruebas}}
{{/each}}
</template>
见iterate through object properties
我正在使用以下出版物来汇总我的产品的有效试用:
Meteor.publish('pruebasActivas', function() {
var pruebasActivas = Clientes.aggregate([
{
$match: {
'saldoPrueba': {
'$gt': 0
}
}
}, {
$group: {
_id: {
id: '$_id',
cliente: '$cliente'
},
totalPruebas: {
$sum: '$saldoPrueba'
}
}
}
]);
});
if (pruebasActivas && pruebasActivas.length > 0 && pruebasActivas[0]) {
return this.added('aggregate3', 'dashboard.pruebasActivas', pruebasActivas);
}
结果抛出以下对象
{
"0": {
"_id": {
"id": "YByiuMoJ3shBfTyYQ",
"cliente": "Foo"
},
"totalPruebas": 30000
},
"1": {
"_id": {
"id": "6AHsPAHZhbP3fCBBE",
"cliente": "Foo 2"
},
"totalPruebas": 20000
},
"_id": "dashboard.pruebasActivas"
}
我如何使用 Blaze 遍历包含对象的数组以显示 "cliente" 和 "totalPruebas"?
让自己成为一个将对象转换为对象数组的助手,仅使用未命名的顶级键_id
:
Template.myTemplate.helpers({
pruebasActivas: function(){
var ob = myCollection.findOne(); // assuming your collection returns a single object
var clientes = [];
for (var p in ob){
if (ob.hasOwnProperty(p) && p !== "_id"){
// here we flatten the object down to two keys
clientes.push({cliente: ob[p]._id.cliente, totalPruebas: ob[p].totalPruebas});
}
}
return clientes;
}
});
现在你可以做:
<template name="myTemplate">
{{#each pruebasActivas}}
Cliente: {{cliente}}
Total Pruebas: {{totalPruebas}}
{{/each}}
</template>
见iterate through object properties