Lodash _.invertBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Like _.invert, except the inverted object is built from running each value through an optional iteratee. Part of Lodash Object (stable — not deprecated). Each inverted key maps to an array of source keys that produced it. The iteratee is invoked with one argument: (value).

Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.

Syntax

Call _.invertBy. Start with this line.

Lodash
_.invertBy(object, [iteratee])
CallWhat it does
_.invertBy(object, [iteratee])Primary signature.
_.invertBy(object)Work with an object’s keys, values, or paths.

Prefer import invertBy from "lodash/invertBy" when you want a smaller bundle.

Library declarations
Lodash
_.invertBy(object, [iteratee])

Parameters

ParameterDescription
objectRequired. Object to invert
iterateeOptional. Invoked as (value) to build each bucket key

Return Value

Return ValueDescription
*New object of bucket → key arrays

Lodash Examples

Example 1 — Official: Invert Without Iteratee

Each value becomes a bucket of source keys.

Lodash
var object = { a: 1, b: 2, c: 1 };

console.log(_.invertBy(object));
// { '1': ['a', 'c'], '2': ['b'] }
Try It Yourself

How It Works

  1. Bucket. Value 1 collects keys a then c.
  2. Unique. Value 2 collects only b.
  3. Arrays. Even unique values become arrays.
  4. Docs. Matches the official first example.

Example 2 — Official: Custom Bucket Names

The iteratee renames each inverted key.

Lodash
var object = { a: 1, b: 2, c: 1 };

console.log(_.invertBy(object, function (value) {
  return "group" + value;
}));
// { group1: ['a', 'c'], group2: ['b'] }
Try It Yourself

How It Works

  1. Iteratee. Receives each value (1, 2, 1).
  2. Rename. Returns 'group1' / 'group2'.
  3. Collect. Same key lists as the plain form.
  4. Docs. Matches the official second example.

Example 3 — _.invert vs _.invertBy

Choose last-wins vs keep-all-keys.

Lodash
var object = { a: 1, b: 2, c: 1 };

console.log(_.invert(object));
// { '1': 'c', '2': 'b' }

console.log(_.invertBy(object));
// { '1': ['a', 'c'], '2': ['b'] }
Try It Yourself

How It Works

  1. invert. Collision on 1 keeps only 'c'.
  2. invertBy. Keeps both 'a' and 'c'.
  3. When. Unique reverse maps → invert.
  4. When. Grouping / no data loss → invertBy.

Example 4 — Property Shorthand on Object Values

Bucket by a field when values are objects.

Lodash
var users = {
  u1: { role: "admin" },
  u2: { role: "editor" },
  u3: { role: "admin" }
};

console.log(_.invertBy(users, "role"));
// { admin: ['u1', 'u3'], editor: ['u2'] }
Try It Yourself

How It Works

  1. Shorthand. 'role' reads each value’s role.
  2. Buckets. admin and editor collect user ids.
  3. Order. Keys appear in iteration order.
  4. Use. Handy for index maps from id → record objects.

Example 5 — Group Keys by Status Label

A practical reverse index for shared labels.

Lodash
var tasks = {
  t1: "todo",
  t2: "done",
  t3: "todo",
  t4: "done"
};

var byStatus = _.invertBy(tasks);

console.log(byStatus.todo); // ['t1', 't3']
console.log(byStatus.done); // ['t2', 't4']
Try It Yourself

How It Works

  1. Source. Task ids map to status strings.
  2. invertBy. Status becomes the bucket key.
  3. Lists. Each status holds every matching task id.
  4. Why not invert? Duplicate statuses would drop ids.

Browser & Runtime Support

_.invertBy is a Lodash Object helper available in 4.x. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native equivalents loop own keys and push into bucket arrays.

✓ Stable · Lodash 4.x

_.invertBy()

Available in [email protected] full builds and the lodash/invertBy modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.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
_.invertBy Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import invertBy from "lodash/invertBy" for smaller builds. Use invert when you want a flat last-wins map.

Frequently Asked Questions

_.invertBy(object, [iteratee]) is like _.invert, but each inverted value is an array of source keys that produced it. Official: _.invertBy({ a: 1, b: 2, c: 1 }) → { '1': ['a', 'c'], '2': ['b'] }.
_.invert returns flat { value: key } pairs and drops earlier keys on collisions. _.invertBy keeps every key in an array bucket.
No. Without it, the original value becomes the bucket key. Pass an iteratee to derive bucket names — official docs use 'group' + value.
No. It returns a new object. The source is untouched.
Yes. Iteratee results are coerced to string keys, so object-typed results can collide as '[object Object]'.
Use import invertBy from "lodash/invertBy" or require("lodash/invertBy") so bundlers can tree-shake. Full build: import _ from "lodash" then _.invertBy.

Next: _.invoke()

Invoke a method at a path on an object with arguments.

_.invoke() tutorial →

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