Lodash _.partition() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.partition(collection, predicate) produces [pass, fail] in one pass.
  • Using shorthand predicates on objects—same ergonomics as filter.
  • Trade-offs versus paired filter/reject calls or boolean groupBy.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Skim _.filter() so predicate iteratees feel familiar—partition is the dual-output variant.

  • You understand predicates as functions returning truthy/falsy.
  • You can open Try-it labs or run snippets locally.

Overview

_.partition answers “give me both piles” without walking the collection twice or juggling temporary flags.

Tuple output

Destructuring-friendly [truthy, falsy] arrays.

Single pass

Predicate evaluated once per element.

Iteratee sugar

Reuse matchers from filter/map pipelines.

Syntax

javascript
_.partition(collection, predicate)
  • collection: array or plain object Lodash can iterate.
  • predicate: invoked as (value, index|key, collection); truthy routes to bucket 0.
  • Returns: [passArray, failArray]—both are new arrays.
1

Separate even and odd numbers

First tuple holds predicate successes—here remainders equal zero.

javascript
import partition from "lodash/partition";

partition([1, 2, 3, 4], (n) => n % 2 === 0);
// → [[2, 4], [1, 3]]
Try it Yourself
2

Shorthand: partition by done

A string iteratee reads that property—truthy values land in the first bucket.

javascript
import partition from "lodash/partition";

partition(
  [
    { id: 1, label: "Write", done: true },
    { id: 2, label: "Ship", done: false }
  ],
  "done"
);
// → [[completed rows], [pending rows]]
Try it Yourself
3

Split strings by length rule

Anything longer than two characters becomes its own pile.

javascript
import partition from "lodash/partition";

partition(["hi", "hello", "x"], (word) => word.length > 2);
// → [["hello"], ["hi", "x"]]
Try it Yourself

📋 _.partition vs filter/reject, groupBy

APIOutputBest when
_.partition(collection, predicate)[pass, fail]You always need both groups from one predicate
_.filter + _.rejectTwo independent arraysConvenient if passes happen far apart in code
_.groupBy(collection, predicate)Keys "true"/"false"More than two buckets or dynamic grouping labels

Pitfalls to avoid

Arity

Exactly two buckets

Need three or more piles—compose nested partitions or switch to groupBy.

Perf

Predicate cost

Heavy predicates still run once per element—cache derives before hot loops.

Tuples

Index confusion

Remember index 0 is truthy hits—name destructured vars explicitly.

❓ FAQ

No—it allocates fresh arrays for the pass and fail buckets while leaving inputs untouched.
Exactly two: truthy predicate hits first, falsy second—even when one side is empty.
One partition walks once; separate filter/reject evaluates the predicate twice.
Yes—property names, matchers, and paths behave like other Lodash iteration helpers.
Generally preserves visitation order—verify fixtures when deterministic ties matter.

Summary

Did you know?

_.partition always returns a two-element array: index 0 holds elements where the predicate was truthy, index 1 holds the rest—ideal for destructuring const [pass, fail] = _.partition(...).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful