Lodash – partition method

Lodash - partition method

Syntax Of Lodash partition method

_.partition(collection, [predicate=_.identity])

Lodash partition method 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 falsely for. The predicate is invoked with one argument: (value).

Arguments

  • collection (Array|Object) โˆ’ The collection to iterate over.
  • [predicate=_.identity] (Function) โˆ’ The function invoked per iteration.

Output

  • (Array) โˆ’ Returns the array of grouped elements.

Example

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.

Command

\>node tester.js

Output

[
   [
     { user: 'Joe', age: 48, active: false },
     { user: 'Julie', age: 40, active: false }
   ],
   [
     { user: 'Robert', age: 34, active: true },
     { user: 'Stafey', age: 36, active: true }
   ]
]

Next Topic – Click Here

Leave a Reply