javascript 对对象数组进行分组和排序的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20330050/
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
Best way to group and sort an array of objects
提问by André
I have the following demo data.
我有以下演示数据。
var demoData= [{"EntryGroupDate":"November 2013",
"DisplayName": "Hans Meier (November)",
"EntryGroupDateSort": 11},
{"EntryGroupDate":"August 2013",
"DisplayName": "Franz Mueller (August)",
"EntryGroupDateSort": 8},
{"EntryGroupDate":"November 2013",
"DisplayName": "Franz Huber (November)",
"EntryGroupDateSort": 11},
{"EntryGroupDate":"Juli 2013",
"DisplayName": "Franz Schmidt (Juli)",
"EntryGroupDateSort": 7}
];
What would be the best way to group them first by EntryGroupDateSort
and sort them afterwards by the same criteria. For the output I need all the information of the original array.
首先将它们分组EntryGroupDateSort
并随后根据相同的标准对它们进行排序的最佳方法是什么。对于输出,我需要原始数组的所有信息。
I have just fiddled around with UnderscoreJS but did not get the desired result by using the following code.
我刚刚摆弄了 UnderscoreJS,但没有通过使用以下代码得到想要的结果。
var myPersons = demoData;
var groups = _.groupBy(myPersons, "EntryGroupDate");
console.log(groups);
groups = _(groups).sortBy(function (item) {
return item.EntryGroupDateSort;
});
console.log(groups);
The first console output shows the data in the format I would like to have after sorting the data.
第一个控制台输出以我希望对数据进行排序后的格式显示数据。
Hopefully someone can point me in the right direction.
希望有人能指出我正确的方向。
回答by MT0
You can do the sort across the entire array first and then group by:
您可以先对整个数组进行排序,然后按以下方式分组:
var demoData= [{"EntryGroupDate":"November 2013",
"DisplayName": "Hans Meier (November)",
"EntryGroupDateSort": 11},
{"EntryGroupDate":"August 2013",
"DisplayName": "Franz Mueller (August)",
"EntryGroupDateSort": 8},
{"EntryGroupDate":"November 2013",
"DisplayName": "Franz Huber (November)",
"EntryGroupDateSort": 11},
{"EntryGroupDate":"Juli 2013",
"DisplayName": "Franz Schmidt (Juli)",
"EntryGroupDateSort": 7}
];
_.sortBy( demoData, function(x){ return x.EntryGroupDateSort; } );
groups = _.groupBy( demoData, 'EntryGroupDate' );
console.log( groups );
If you want to sort after grouping then you can use (sorting on DisplayName
this time):
如果要在分组后排序,则可以使用(此时排序DisplayName
):
groups = _.groupBy( demoData, 'EntryGroupDate' );
for ( var key in groups )
groups[key].sort( function(a,b){ var x = a.DisplayName, y = b.DisplayName; return x === y ? 0 : x < y ? -1 : 1; } );
console.log( groups );
回答by Anand Jha
Try this,
试试这个,
demoData.sort(function(a, b) {
var textA = a.EntryGroupDateSort;
var textB = b.EntryGroupDateSort;
return (textA < textB) ? 1 : (textA > textB) ? -1 : 0;
//return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; //for ascending.
});
回答by idbehold
The _.groupBy()
function returns an object and you cannot "sort" an object. The keys/values of an object do not have any order.The _.sortBy()
method transforms your object (containing only arrays)into an array of arrays and sorts the values based on EntryGroupDateSort
. The problem is that you're trying to sort the array of arrays by the EntryGroupDateSort
value (which isn't a property of an array). What you want to do is sort the grouped object of arrays by the _.first()
item in each array. Here's the closest you're going to get to what you want:
该_.groupBy()
函数返回一个对象,您不能“排序”一个对象。对象的键/值没有任何顺序。该_.sortBy()
方法将您的对象(仅包含数组)转换为数组数组并基于 对值进行排序EntryGroupDateSort
。问题是您试图按EntryGroupDateSort
值(它不是数组的属性)对数组数组进行排序。您想要做的是按_.first()
每个数组中的项目对数组的分组对象进行排序。这是最接近你想要的东西:
var groups = _(demoData).chain()
.groupBy("EntryGroupDate")
.tap(function(data){ console.log(data) })
.sortBy(function(data){
// Note that we're sorting by EntryGroupDateSort
// from the _.first() item in the array
return _.first(data).EntryGroupDateSort
})
.tap(function(data){ console.log(data) })
.value();