Lodash _.shuffle() method
What you’ll learn
- How
_.shuffle(collection)returns a new array containing every member in random order. - Why source arrays and strings stay unchanged while you iterate the shuffled copy.
- When smaller helpers (
sample,sampleSize) beat full permutations. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.sampleSize() for partial random draws—shuffle always consumes the whole finite pool.
- You accept pseudo-random ordering for UX—not security primitives.
- You can open Try-it labs or run snippets locally.
Overview
_.shuffle is the kitchen-sink random ordering helper—flashcard decks, playlist reshuffles, randomized audit queues—without rewriting Fisher–Yates yourself.
Full permutation
Every element appears exactly once—multiplicity mirrors the source collection.
Immutable inputs
Original arrays/objects/strings remain stable—only the returned array is reordered.
Collections beyond arrays
Shuffle enumerable object values or characters pulled from strings.
Syntax
_.shuffle(collection) - collection: array, array-like string, or plain object Lodash can iterate.
- Returns: new array whose elements are a random permutation of the collection’s members.
- Randomness: pseudorandom—swap for crypto primitives when stakes are high.
Shuffle a numeric array
The multiset stays intact—only ordering changes—and the source array keeps its original sequence.
import shuffle from "lodash/shuffle";
const deck = [1, 2, 3, 4];
shuffle(deck);
// → permutation of [1,2,3,4]; deck still [1,2,3,4] Shuffle values from a plain object
Keys determine enumeration order inputs only—the output is always an array of shuffled values.
import shuffle from "lodash/shuffle";
shuffle({ alpha: "x", beta: "y", gamma: "z" });
// → permutation of ["x","y","z"] Shuffle characters in a string
Strings degrade into character tiles—useful for toy anagram generators alongside Unicode caveats for surrogate pairs.
import shuffle from "lodash/shuffle";
shuffle("abc");
// → array of "a", "b", "c" in random order 📋 _.shuffle vs sample, sampleSize, manual sorts
| API | Outcome | Best when |
|---|---|---|
_.shuffle(collection) | Full-length permutation array | You need every element eventually, just randomized |
_.sample(collection) | Single element | Only one winner matters |
_.sampleSize(collection, n) | n draws without exhausting the pool twice | Polls or spot checks |
array.sort(() => Math.random() - 0.5) | Biased ordering | Avoid—uniformity bugs; prefer shuffle helpers |
Pitfalls to avoid
Predictable decks
Card games for fun are fine; keyed tokens or lottery draws need audited RNGs.
Flaky specs
Stub Math.random in focused tests or wrap shuffle behind your own function—Lodash exposes no seed knob.
Grapheme surprises
Emoji and combining marks may span multiple UTF-16 units—shuffle sees code units, not human glyphs.
❓ FAQ
Summary
- Purpose:
_.shuffle(collection)returns a new array containing all members in pseudo-random order. - Contrast: take
sample/sampleSizewhen you only need a subset. - Next: Lodash _.size(), Lodash _.sampleSize() (previous), or collection hub.
_.shuffle returns a new array—your original array, object, or string literal is never reordered in place, making it safe beside immutable UI state patterns.
6 people found this page helpful
