如何按属性对我的集合进行分组?

时间:2013-12-13 13:15:11

标签: meteor collections

我收藏了

 {categories:"category 1",name:"etc1"},{categories:"category 1",name:"etc2"},{categories:"category 2",name:"etc3"},{categories:"category 2",name:"etc"}.

所以,在Fetch中...我如何按类别分组?

1 个答案:

答案 0 :(得分:2)

流星不能单独执行此操作,但您可以使用下划线来执行此操作:

//results from .fetch()
var results = [{categories:"category 1",name:"etc1"},{categories:"category 1",name:"etc2"}....

var grouped = _.groupBy(results, function(item) {
                  return item.categories;
              });

你应该得到这样的东西:

{"category 1":[{"categories":"category 1","name":"etc1"},{"categories":"category 1","name":"etc2"}],"category 2":[{"categories":"category 2","name":"etc3"},{"categories":"category 2","name":"etc"}]}