Skip to main content

Arrays

Runtime: any — Node.js ≥ 22 or a modern browser

Small, non-mutating helpers for everyday array work — chunking, deduping, compacting, shuffling, splitting, sorting and pairing. Every function returns a new array and leaves its input untouched, including shuffle, which copies before running its Fisher-Yates pass. unique dedupes through a Set, so equality is SameValueZero — primitives compare by value, objects by reference, and two structurally identical objects both survive.

sortBy reads its key once per element and falls back to the original index for ties, so it is stable and — unlike Array#sort, which stringifies — numeric keys sort numerically.

first, last, flatten and groupBy were removed in 4.0 in favour of at(0), at(-1), flat() and Object.groupBy, and they stay removed: the platform covers them exactly. Group with Object.groupBy(items, keyFn) (Node 22+).

Example​

import { chunk, compact, partition, sortBy, unique, zip } from '@rtorcato/js-common/arrays'

// Send 500 ids to an API that accepts 50 at a time.
for (const batch of chunk(unique(ids), 50)) {
await api.post('/bulk', { ids: batch })
}

compact([1, 0, 2, null, 3]) // [1, 2, 3] — every falsy value dropped, including 0

const [active, archived] = partition(users, (u) => u.active)
sortBy(candles, (c) => c.date) // stable, non-mutating, chronological
zip(labels, values) // [['a', 1], ['b', 2]]

Import​

import { chunk, compact, partition } from '@rtorcato/js-common/arrays'

Exports​

NameSummary
chunkChunks an array into smaller arrays of a specified size.
compactRemoves all falsy values from an array.
partitionSplits an array into two arrays: the elements that satisfy the predicate and the ones that do not.
shuffleShuffles an array using the Fisher-Yates algorithm.
sortBySorts an array by a derived key, without mutating the input.
uniqueRemoves duplicate values from an array while preserving order.
zipPairs up two arrays element by element.

See also​

  • objects — pick, omit, deepMerge and other object helpers
  • maps — merge, invert and convert Maps
  • random — random ints, floats, strings and array picks