Lodash partition method
语法:
_.partition(collection, [predicate=_.identity])
Creates an array of elements split into two groups, the first of which contains elements predicate returns truthy for, the second of which contains elements predicate returns falsey for. The predicate is invoked with one argument: (value).
参数:
collection (Array|Object) − The collection to iterate over.
[predicate=_.identity] (Function) − The function invoked per iteration.
输出:
(Array) − Returns the array of grouped elements.
例子:
var _ = require('lodash');
var users = [
{ user: 'Joe', age: 48, active: false },
{ user: 'Robert', age: 34, active: true },
{ user: 'Julie', age: 40, active: false },
{ user: 'Stafey', age: 36, active: true }
];
var result = _.partition(users, ['active', false]);
console.log(result);
Save the above program in
tester.js. Run the following command to execute this program.
命令:
输出:
[
[
{ user: 'Joe', age: 48, active: false },
{ user: 'Julie', age: 40, active: false }
],
[
{ user: 'Robert', age: 34, active: true },
{ user: 'Stafey', age: 36, active: true }
]
]