I have an array with keys like so:
我有一个带键的数组:
['asdf12','39342aa','12399','129asg',...]
and a collection which has these keys in each object like so:
在每个对象中都有这些键的集合如下:
[{guid: '39342aa', name: 'John'},{guid: '129asg', name: 'Mary'}, ... ]
Is there a fast way to sort the collection based on the order of keys in the first array?
是否有一种快速的方法根据第一个数组中的键的顺序对集合进行排序?
3 个解决方案
#1
22
var sortedCollection = _.sortBy(collection, function(item){
return firstArray.indexOf(item.guid)
});
#2
5
Input:
输入:
var data1 = ['129asg', '39342aa'];
var data2 = [{
guid: '39342aa',
name: 'John'
}, {
guid: '129asg',
name: 'Mary'
}];
-
First create an index object, with
_.reduce
, like this首先创建一个索引对象,使用_。减少,像这样
var indexObject = _.reduce(data2, function(result, currentObject) { result[currentObject.guid] = currentObject; return result; }, {});
-
And then
map
the items of the first array with the objects from theindexObject
, like this然后将第一个数组的项与来自indexObject的对象映射,如下所示
console.log(_.map(data1, function(currentGUID) { return indexObject[currentGUID] }));
Output
输出
[ { guid: '129asg', name: 'Mary' },
{ guid: '39342aa', name: 'John' } ]
Note: This method will be very efficient if you want to sort so many objects, because it will reduce the linear look-up in the second array which would make the entire logic run in O(M * N) time complexity.
注意:如果您想对这么多对象进行排序,这个方法将非常有效,因为它将减少第二个数组中的线性查找,这将使整个逻辑在O(M * N)时间复杂度中运行。
#3
2
You can use indexBy(), and at() to sort your collection. The advantage being that concise code and performance. Using sortBy()
here does the trick, but your external array is already sorted:
可以使用indexBy()和at()对集合进行排序。优点是简洁的代码和性能。这里使用sortBy()可以达到这个目的,但是您的外部数组已经被排序:
var ids = [ 'cbdbac14', 'cf3526e2', '189af064' ];
var collection = [
{ guid: '189af064', name: 'John' },
{ guid: 'cf3526e2', name: 'Julie' },
{ guid: 'cbdbac14', name: 'James' }
];
_(collection)
.indexBy('guid')
.at(ids)
.pluck('name')
.value();
// → [ 'James', 'Julie', 'John' ]
Using at()
, you can iterate over the sorted external collection, building a new collection from the source collection
. The source collection has been transformed into an object using indexBy()
. You do this so at() has key-based access for each of it's ids
.
使用at(),可以对已排序的外部集合进行迭代,从源集合构建新的集合。使用indexBy()将源集合转换为对象。这样做,at()对每个id都有基于键的访问。
#1
22
var sortedCollection = _.sortBy(collection, function(item){
return firstArray.indexOf(item.guid)
});
#2
5
Input:
输入:
var data1 = ['129asg', '39342aa'];
var data2 = [{
guid: '39342aa',
name: 'John'
}, {
guid: '129asg',
name: 'Mary'
}];
-
First create an index object, with
_.reduce
, like this首先创建一个索引对象,使用_。减少,像这样
var indexObject = _.reduce(data2, function(result, currentObject) { result[currentObject.guid] = currentObject; return result; }, {});
-
And then
map
the items of the first array with the objects from theindexObject
, like this然后将第一个数组的项与来自indexObject的对象映射,如下所示
console.log(_.map(data1, function(currentGUID) { return indexObject[currentGUID] }));
Output
输出
[ { guid: '129asg', name: 'Mary' },
{ guid: '39342aa', name: 'John' } ]
Note: This method will be very efficient if you want to sort so many objects, because it will reduce the linear look-up in the second array which would make the entire logic run in O(M * N) time complexity.
注意:如果您想对这么多对象进行排序,这个方法将非常有效,因为它将减少第二个数组中的线性查找,这将使整个逻辑在O(M * N)时间复杂度中运行。
#3
2
You can use indexBy(), and at() to sort your collection. The advantage being that concise code and performance. Using sortBy()
here does the trick, but your external array is already sorted:
可以使用indexBy()和at()对集合进行排序。优点是简洁的代码和性能。这里使用sortBy()可以达到这个目的,但是您的外部数组已经被排序:
var ids = [ 'cbdbac14', 'cf3526e2', '189af064' ];
var collection = [
{ guid: '189af064', name: 'John' },
{ guid: 'cf3526e2', name: 'Julie' },
{ guid: 'cbdbac14', name: 'James' }
];
_(collection)
.indexBy('guid')
.at(ids)
.pluck('name')
.value();
// → [ 'James', 'Julie', 'John' ]
Using at()
, you can iterate over the sorted external collection, building a new collection from the source collection
. The source collection has been transformed into an object using indexBy()
. You do this so at() has key-based access for each of it's ids
.
使用at(),可以对已排序的外部集合进行迭代,从源集合构建新的集合。使用indexBy()将源集合转换为对象。这样做,at()对每个id都有基于键的访问。