Lodash partition Method: Split Arrays in JavaScript
This article provides a comprehensive guide on how the
_.partition method works in the Lodash JavaScript library.
You will learn the syntax of the method, understand its internal
evaluation mechanism, and see practical examples illustrating how it
splits a collection into two distinct groups based on a predicate
function.
What is Lodash
_.partition?
The _.partition method splits an array (or collection)
into two separate arrays:
- The first array contains all elements for which the predicate function returns truthy.
- The second array contains all elements for which the predicate function returns falsy.
Both resulting arrays are wrapped inside a single parent array:
[truthyArray, falsyArray].
Syntax
_.partition(collection, [predicate=_.identity])collection: The array or object to iterate over.[predicate=_.identity]: The function or shorthand invoked per iteration to determine which array the current element belongs to.- Returns: A new array containing two arrays:
[truthyElements, falsyElements].
How It Works
Under the hood, _.partition iterates through the
provided collection exactly once (an \(O(n)\) operation). For each element, it
evaluates the predicate:
- If the predicate returns a truthy value (
true, a non-zero number, a non-empty string, an object, etc.), the element is appended to the first array. - If the predicate returns a falsy value (
false,0,"",null,undefined,NaN), the element is appended to the second array.
This approach is more performant than using JavaScript's native
Array.prototype.filter() twice, which requires iterating
through the entire list two separate times.
Examples
1. Splitting Numbers (Even vs. Odd)
You can pass a custom callback function as the predicate to split primitive values:
const _ = require('lodash');
const numbers = [1, 2, 3, 4, 5, 6, 7, 8];
const [evens, odds] = _.partition(numbers, (n) => n % 2 === 0);
console.log(evens); // Output: [2, 4, 6, 8]
console.log(odds); // Output: [1, 3, 5, 7]2. Splitting Objects Using Property Shorthands
Lodash supports property name shorthands. If a string is passed as
the predicate, _.partition checks the truthiness of that
property on each object:
const users = [
{ user: 'Alice', active: true },
{ user: 'Bob', active: false },
{ user: 'Charlie', active: true },
{ user: 'Dave', active: false }
];
const [activeUsers, inactiveUsers] = _.partition(users, 'active');
console.log(activeUsers);
// Output: [{ user: 'Alice', active: true }, { user: 'Charlie', active: true }]
console.log(inactiveUsers);
// Output: [{ user: 'Bob', active: false }, { user: 'Dave', active: false }]3. Splitting by Key-Value Matches
You can pass an object to match specific key-value pairs:
const employees = [
{ name: 'Sarah', role: 'developer' },
{ name: 'John', role: 'designer' },
{ name: 'Emma', role: 'developer' }
];
const [developers, nonDevelopers] = _.partition(employees, { role: 'developer' });
console.log(developers);
// Output: [{ name: 'Sarah', role: 'developer' }, { name: 'Emma', role: 'developer' }]
console.log(nonDevelopers);
// Output: [{ name: 'John', role: 'designer' }]Summary
The _.partition method offers an efficient, declarative
way to divide datasets into two groups without writing manual loops or
running multiple filter passes. Destructuring the returned array
(const [pass, fail] = _.partition(...)) is standard
practice to immediately access both resulting sets.