javascript 如何在 Backbone.js 中使用 groupBy 对集合进行分组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9954597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:20:34  来源:igfitidea点击:

How to use groupBy in Backbone.js to group collections?

javascriptbackbone.jsunderscore.js

提问by Mark Gia Bao Nguyen

An example collection (showing models only):

示例集合(仅显示模型):

[
    {
        name: "Bob"
        date: "Thu Mar 29 2012"
    },
    {
        name: "James"
        date: "Fri Mar 30 2012"
    },
    {
        name: "Dylan"
        date: "Fri Mar 30 2012"
    },
    {
        name: "Stanley"
        date: "Sat Mar 31 2012"
    },
]

How can I use Underscore.js' groupBy function to group models with the same date?

如何使用 Underscore.js 的 groupBy 函数对具有相同日期的模型进行分组?

采纳答案by ThiefMaster

Use _.groupBy(data, 'date');

利用 _.groupBy(data, 'date');

You could also use pass a custom function, but in this case it's not necessary as the attribute shortcut syntax above works fine.

您也可以使用 pass 自定义函数,但在这种情况下,它没有必要,因为上面的属性快捷语法可以正常工作。

_.groupBy(data, function(row) {
    return row.date;
});

Demo:

演示:

> _.groupBy(data, 'date')
{ 'Thu Mar 29 2012': [ { name: 'Bob', date: 'Thu Mar 29 2012' } ],
  'Fri Mar 30 2012':
   [ { name: 'James', date: 'Fri Mar 30 2012' },
     { name: 'Dylan', date: 'Fri Mar 30 2012' } ],
  'Sat Mar 31 2012': [ { name: 'Stanley', date: 'Sat Mar 31 2012' } ] }
> _.groupBy(data, function(row) { return row.date });
{ 'Thu Mar 29 2012': [ { name: 'Bob', date: 'Thu Mar 29 2012' } ],
  'Fri Mar 30 2012':
   [ { name: 'James', date: 'Fri Mar 30 2012' },
     { name: 'Dylan', date: 'Fri Mar 30 2012' } ],
  'Sat Mar 31 2012': [ { name: 'Stanley', date: 'Sat Mar 31 2012' } ] }
>

回答by bullfrog

If you are grouping an actual backbone collection you can use the backbone method groupBy which implicitly uses underscore _.groupBy functionality. This is a much cleaner approach in my opinion.

如果您正在对实际的主干集合进行分组,则可以使用隐式使用下划线 _.groupBy 功能的主干方法 groupBy。在我看来,这是一种更清洁的方法。

collection.groupBy( function(model){
  return model.get('date');
});

回答by RaTiO

ThiefMaster answer is perfectly valid, but it caused me some confusion because I was searching a solution for a backbone.js collection as the title indicates.

ThiefMaster 的回答是完全有效的,但它引起了我的一些困惑,因为我正在搜索一个backbone.js 集合的解决方案,如标题所示。

If the question object is a backbone collection we should do the following to group the models by date:

如果问题对象是主干集合,我们应该执行以下操作以按日期对模型进行分组:

_.groupBy(collection.models, function(model){
    return model.get('date')
});

I hope it helps

我希望它有帮助

回答by Jonatan Anauati

var groups =collection.groupBy(function(model) { return model.get('date'); });

//iterate over groups
for(date in groups) { 
    var models = groups[date];
    console.log('date: '+date); 
    for (var model_idx in models) {  
        console.log('    name: '+ models[model_idx].get('name'));
    }
}

回答by Andrew

For those using Lodashinstead of underscore, you can achieve the same result using the _.method()accessor,

对于那些使用Lodash而不是下划线的人,您可以使用_.method()访问器获得相同的结果,

collection.groupBy(_.method('get', 'date'));