Lodash教程

Lodash orderBy method

语法:

_.orderBy(collection, [iteratees=[_.identity]], [orders])
This method is like _.sortBy except that it allows specifying the sort orders of the iteratees to sort by. If orders is unspecified, all values are sorted in ascending order. Otherwise, specify an order of "desc" for descending or "asc" for ascending sort order of corresponding values.

参数:

collection (Array|Object) − The collection to iterate over. [iteratees=[_.identity]] (Array[]|Function[]|Object[]|string[]) − The iteratees to sort by. [orders] (string[]) − The sort orders of iteratees.

输出:

(Array) − Returns the new sorted array.

例子:

var _ = require('lodash');
var users = [
   { 'user': 'Joe', 'age': 48 },
   { 'user': 'Robert', 'age': 34 },
   { 'user': 'Julie', 'age': 40 },
   { 'user': 'Stafey', 'age': 36 }
];
var result = _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
console.log(result);
Save the above program in tester.js. Run the following command to execute this program.

命令:

\>node tester.js

输出:

[
   { user: 'Joe', age: 48 },
   { user: 'Julie', age: 40 },
   { user: 'Robert', age: 34 },
   { user: 'Stafey', age: 36 }
]
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4