[英]Lodash - Sort by position in array
I have an array of objects 我有一系列对象
let myArray = [
{
id: 'first',
name: 'john',
},
{
id: 'second',
name: 'Emmy',
},
{
id: 'third',
name: 'Lazarus',
}
]
and an array 和一个数组
let sorter = ['second', 'third', 'first']
I would like to use lodash
sorting method to sort my objects according to their position in sorter
. 我想使用lodash
排序方法根据对象在sorter
的位置对对象进行sorter
。 So that the output would be 这样输出将是
let mySortedArray = [
{
id: 'second',
name: 'Emmy',
},
{
id: 'third',
name: 'Lazarus',
},
{
id: 'first',
name: 'john',
}
]
Is it possible to do so? 有可能这样做吗?
You can achieve this by using map and find : 您可以通过使用map和find来实现:
let myArray = [ { id: "first", name: "john" }, { id: "second", name: "Emmy" }, { id: "third", name: "Lazarus" } ]; let sorter = ["second", "third", "first"]; let mySortedArray = sorter.map(x => myArray.find(y => y.id === x)); console.log(mySortedArray);
Using lodash you can use _.sortBy 使用lodash可以使用_.sortBy
let myArray = [ { id: 'first', name: 'john', }, { id: 'second', name: 'Emmy', }, { id: 'third', name: 'Lazarus', } ] let sorter = ['second', 'third', 'first'] console.log(_.sortBy(myArray,(i) => {return sorter.indexOf(i.id)}))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
If you want to sort the array in-place, you don't need Lodash, you can easily do it with vanilla JavaScript 如果您想就地对数组进行排序,则不需要Lodash,可以使用原始JavaScript轻松完成
let myArray = [ { id: 'first', name: 'john', }, { id: 'second', name: 'Emmy', }, { id: 'third', name: 'Lazarus', } ] let sorter = ['second', 'third', 'first'] //create a lookup table (map) to save looking through the array const sortLookup = new Map(); //populate with element as key - index as value sorter.forEach((id, index) => sortLookup.set(id, index)); //sort using the indexes of sorter myArray.sort((a, b) => sortLookup.get(a.id) - sortLookup.get(b.id)) console.log(myArray)
This is using a Map but the same can easily be accomplished with a plain JavaScript Object {}
. 这使用的是Map,但使用普通的JavaScript Object {}
可以轻松实现相同功能。 You don't even need to pre-compute the lookup myArray.sort((a, b) => sorter.indexOf(a.id) - sorter.indexOf(b.id))
would give the exact same output but it would mean that instead of traversing sorter
once for a complexity of O(n)
, you potentially have O(n^m)
or O(n^n)
(if both arrays are the same length) 您甚至不需要预先计算查找myArray.sort((a, b) => sorter.indexOf(a.id) - sorter.indexOf(b.id))
得到完全相同的输出,但是它将意味着您不必具有O(n)
的复杂度就可以遍历sorter
一次,而可能拥有O(n^m)
或O(n^n)
(如果两个数组的长度相同)
Since you have an index
array in the case of the sorter
you can _.keyBy
the main array and then use the sorter
to access by index: 由于在sorter
有index
数组,因此可以_.keyBy
主数组,然后使用sorter
按索引访问:
let myArray = [ { id: 'first', name: 'john', }, { id: 'second', name: 'Emmy', }, { id: 'third', name: 'Lazarus', } ] let sorter = ['second', 'third', 'first'] const idMap = _.keyBy(myArray, 'id') const result = _.map(sorter, x => idMap[x]) console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
This should perform better since you only do the idMap once
and then access it by index
. 这应该会更好,因为您只需执行idMap once
,然后access it by index
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.