The lodash method partition exported as a module for splitting collections into two groups based on a predicate function
npx @tessl/cli install tessl/npm-lodash-partition@4.6.0Lodash Partition provides the partition method from the lodash library as a standalone module. It creates an array of elements split into two groups based on the result of running each element through a predicate function. The first group contains elements for which the predicate returns truthy, and the second group contains elements for which the predicate returns falsey.
npm install lodash.partitionvar partition = require('lodash.partition');For ES modules (if using a bundler):
import partition from 'lodash.partition';var partition = require('lodash.partition');
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true },
{ 'user': 'pebbles', 'age': 1, 'active': false }
];
// Function predicate
partition(users, function(o) { return o.active; });
// => [
// [{ 'user': 'fred', 'age': 40, 'active': true }],
// [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]
// Property path shorthand
partition(users, 'active');
// => [
// [{ 'user': 'fred', 'age': 40, 'active': true }],
// [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]Creates an array of elements split into two groups based on the result of running each element through a predicate function.
/**
* 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).
*
* @param {Array|Object} collection The collection to iterate over.
* @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
* @returns {Array} Returns the array of grouped elements.
*/
function partition(collection, predicate);Parameters:
collection (Array|Object): The collection to iterate over[predicate=_.identity] (Function|Object|string): The function invoked per iterationReturns:
Predicate Formats:
The predicate parameter supports multiple formats for maximum flexibility:
Function: Custom predicate function
partition(users, function(o) { return o.active; });Object: The _.matches iteratee shorthand
partition(users, { 'age': 1, 'active': false });Array: The _.matchesProperty iteratee shorthand
partition(users, ['active', false]);String: The _.property iteratee shorthand
partition(users, 'active');Usage Examples:
var partition = require('lodash.partition');
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true },
{ 'user': 'pebbles', 'age': 1, 'active': false }
];
// Using function predicate
var result1 = partition(users, function(o) { return o.active; });
// => [
// [{ 'user': 'fred', 'age': 40, 'active': true }],
// [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]
// Using object predicate (matches properties)
var result2 = partition(users, { 'age': 1, 'active': false });
// => [
// [{ 'user': 'pebbles', 'age': 1, 'active': false }],
// [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'fred', 'age': 40, 'active': true }]
// ]
// Using array predicate (property-value pair)
var result3 = partition(users, ['active', false]);
// => [
// [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }],
// [{ 'user': 'fred', 'age': 40, 'active': true }]
// ]
// Using string predicate (property path)
var result4 = partition(users, 'active');
// => [
// [{ 'user': 'fred', 'age': 40, 'active': true }],
// [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]
// Working with arrays
var numbers = [1, 2, 3, 4, 5, 6];
var evenOdd = partition(numbers, function(n) { return n % 2 === 0; });
// => [[2, 4, 6], [1, 3, 5]]
// Working with objects
var obj = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 };
var objPartition = partition(obj, function(value) { return value > 2; });
// => [[3, 4], [1, 2]]module.exports = partition)