Underscore.js _.partition() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Splits a collection into two arrays: one whose elements all satisfy the predicate, and one whose elements all do not (official Underscore docs). Stable Collection helper — not deprecated.

Pass the required arguments shown below. Underscore collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.partition. Start with this line.

Underscore
_.partition(list, predicate)
CallWhat it does
_.partition(list, predicate)Primary signature.
_.partition(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.partition(list, predicate)

Parameters

ParameterDescription
listRequired. Collection to split
predicateRequired. Function or property name (truthy test)
contextOptional. this for the predicate

Return Value

Return ValueDescription
*[[pass], [fail]]

Underscore.js Examples

Example 1 — Odds and Evens

Split numbers with a predicate — passers first, failers second.

Underscore
_.partition([0, 1, 2, 3, 4, 5], function (n) {
  return n % 2 !== 0;
});
// → [[1, 3, 5], [0, 2, 4]]
Try It Yourself

How It Works

  1. Test each number. Odd → pass; even → fail.
  2. Group 0. Odds collect as [1, 3, 5].
  3. Group 1. Evens collect as [0, 2, 4].
  4. Order kept. Relative order inside each group matches the source.

Example 2 — Split by Property

Pass a property name to split by truthiness of that field.

Underscore
var users = [
  { name: "Ada", active: true },
  { name: "Linus", active: false },
  { name: "Grace", active: true }
];

_.partition(users, "active");
// → [[{Ada…}, {Grace…}], [{Linus…}]]
Try It Yourself

How It Works

  1. Shorthand. "active" means “is item.active truthy?”
  2. Passers. Ada and Grace land in group 0.
  3. Failers. Linus lands in group 1.
  4. No extra loop. You get both sets without a second filter.

Example 3 — Pass / Fail Threshold

Name the two groups after partitioning scores.

Underscore
var scores = [82, 41, 67, 30, 95];
var parts = _.partition(scores, function (s) {
  return s >= 50;
});
var passed = parts[0];  // [82, 67, 95]
var failed = parts[1];  // [41, 30]
Try It Yourself

How It Works

  1. Threshold. Scores ≥ 50 pass the predicate.
  2. Unpack. Assign parts[0] and parts[1] to clear names.
  3. Use both. Report passers and failers without rescanning.
  4. Stable order. Each group follows source order.

Example 4 — All Pass or Empty List

You still get two arrays — one (or both) may be empty.

Underscore
_.partition([2, 4, 6], function (n) {
  return n % 2 === 0;
});
// → [[2, 4, 6], []]

_.partition([], function () { return true; });
// → [[], []]
Try It Yourself

How It Works

  1. All pass. Fail group is [], not omitted.
  2. Empty source. Both groups are empty arrays.
  3. Safe destructure. You can always read parts[0] and parts[1].
  4. No throw. Edge cases stay predictable.

Browser & Runtime Support

_.partition ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.partition()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.partition Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]). Native alternative: one loop pushing into two arrays, or pair filter with its complement.

Frequently Asked Questions

_.partition(list, predicate) splits a collection into two arrays: elements that pass the predicate, then elements that fail. The result is [[pass], [fail]].
_.filter returns only the passers. _.partition returns both passers and failers in one pass — like filter plus reject together.
Yes. Pass a string shorthand such as 'active' to split by the truthiness of that property on each item.
Yes. Within each group, elements keep their original relative order.
You still get two arrays. All-pass yields [[...items], []]. Empty input yields [[], []].
No. It returns two new arrays; the source is unchanged.

Next: Collections hub

Browse every Underscore.js collection helper in one place.

Collection methods →

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.

10 people found this page helpful