Lodash _.rearg() method

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

What you’ll learn

  • How indexes maps wrapper arguments into func’s parameter slots.
  • How _.rearg generalizes simple swaps beyond _.flip.
  • Patterns for adapting callbacks to mismatched library signatures.
  • Pitfalls when arity, duplicates, or sparse indexes confuse readers.

Prerequisites

Review _.flip(), _.partialRight(), and the Function hub.

  • Arrays as tuples: treating ordered arguments like indexed slots.
  • Permutations: reordering finite sequences.

Overview

_.rearg(func, indexes) returns a wrapper that shuffles arguments according to your index map—ideal when integrating libraries that disagree about parameter order.

Syntax

javascript
_.rearg(func, indexes)
  • func: function whose arguments should be permuted.
  • indexes: array describing which source argument occupies each destination slot.
  • returns: wrapped function applying the permutation per call.
1

Swap two parameters

[1, 0] exchanges the first and second invocation arguments—generalizing a binary flip.

javascript
import rearg from "lodash/rearg";

function concatPair(left, right) {
  return left + ":" + right;
}

const swapped = rearg(concatPair, [1, 0]);

swapped("second", "first"); // "first:second"
2

Rotate three inputs

Lodash’s canonical illustration: [2, 0, 1] pulls the third argument forward.

javascript
import rearg from "lodash/rearg";

function tripleReport(first, second, third) {
  return [first, second, third].join("|");
}

const rotated = rearg(tripleReport, [2, 0, 1]);

rotated("A", "B", "C"); // "C|A|B"
3

Numeric APIs with reversed operands

When an upstream helper passes (minuend, subtrahend) but your core expects the opposite, rearg encodes the swap explicitly.

javascript
import rearg from "lodash/rearg";

function subtract(minuend, subtrahend) {
  return minuend - subtrahend;
}

const subtractReversed = rearg(subtract, [1, 0]);

subtractReversed(3, 10); // 7 → equivalent to subtract(10, 3)

📋 _.rearg vs _.flip vs manual wrappers

ApproachControlsTrade-off
_.reargAny permutation you declareReaders must decode index arrays
_.flipFull reversal onlyCannot express rotations or selective swaps
Manual wrapperArbitrary logicNo shared lodash metadata conventions

Pitfalls to avoid

Readability

Opaque index maps

Annotate non-trivial permutations with comments or small tests—future readers rarely memorize slot algebra instantly.

Arity

Unexpected undefined slots

When callers omit trailing arguments, reordered parameters may receive undefined—mirror defensive checks from the original func.

this

Method adapters

rearg does not bind this; compose with bind when wrapping prototype methods.

❓ FAQ

It wraps func so every call permutes the incoming arguments according to indexes before invoking the original function.
Think destination order: the nth slot passed to func receives args[indexes[n]] from the wrapper invocation.
flip hard reverses all arguments. rearg accepts any permutation—including partial swaps or rotations.
Lodash aligns transforms with how many reorder slots you specify; mind arity when mixing with variadic functions.
Use import rearg from "lodash/rearg" for tree-shaking friendly bundles.

Summary

Did you know?

Each integer in the indexes array picks which incoming argument occupies that destination slot inside func—so [2, 0, 1] feeds args[2] first, args[0] second, and args[1] third, mirroring Lodash’s documented permutation semantics.

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