Collection · 50 utilities

Array utilities

Start with copies and equality, then move to grouping and aggregation. Notice where object identity differs from deep equality.

Clone an array

Create a new array without copying the objects inside it.

function clone(values) { return [...values]; }array

Remove duplicate values

Keep the first occurrence of each value, in its original order.

function unique(values) { return [...new Set(values)]; }array

Flatten a nested array

Turn a nested array into one sequence of values.

function flatten(values) { return values.flat(Infinity); }array

Split an array into chunks

Build consecutive chunks without changing the original array.

function chunk({ values, size }) { return Array.from({ length: Math.ceil(values.length / size) …array

Find the intersection of arrays

Return the distinct values that occur in both arrays.

function intersection({ left, right }) { const other = new Set(right); return [...new Set(left)…array

Sort numbers in ascending order

Sort a numeric copy instead of using JavaScript’s default string ordering.

function sortNumbers(values) { return [...values].sort((a, b) => a - b); }array

Sum an array of numbers

Add numeric values with an explicit result for an empty array.

function sum(values) { return values.reduce((total, value) => total + value, 0); }array

Group objects by a key

Collect records by a string or numeric field without prototype-key collisions.

function groupBy({ values, key }) { const groups = new Map(); for (const value of values) { con…array

Cast a value as an array

Wrap a single value in an array and return arrays unchanged.

function castArray(value) { return Array.isArray(value) ? value : [value]; }array

Get all subsets of an array

Build the power set, smallest subsets first.

function getSubsets(values) { return values.reduce((subsets, value) => subsets.concat(subsets.m…array

Get union of arrays

Merge arrays and keep each value once, in first-seen order.

function union(arrays) { return [...new Set(arrays.flat())]; }array

Swap two array items

Return a copy with the items at i and j exchanged.

function swapItems({ values, i, j }) { const copy = [...values]; [copy[i], copy[j]] = [copy[j],…array

Unzip an array of arrays

Regroup the n-th item of every array into the n-th output array.

function unzip(arrays) { return Array.from({ length: Math.max(0, ...arrays.map(array => array.l…array

Compare two arrays

Check the same length and the same items in the same order.

function isEqual({ a, b }) { return a.length === b.length && a.every((value, i) => Object.is(va…array

Create cartesian product

Combine one item from each set in every possible way.

function cartesian(sets) { return sets.reduce((product, set) => product.flatMap(prefix => set.m…array

Get the rank of an array of numbers

Rank each number by how many values are larger, starting at 1.

function ranking(values) { return values.map(value => values.filter(other => other > value).len…array

Zip multiple arrays

Pair the n-th items of several arrays.

function zip(arrays) { return Array.from({ length: Math.max(0, ...arrays.map(array => array.len…array

Create an array of cumulative sum

Replace each number with the running total up to it.

function accumulate(values) { let total = 0; return values.map(value => (total += value)); }array

Get all arrays of consecutive elements

Slide a window of a given size across an array.

function windows({ values, size }) { return size > values.length ? [] : values.slice(size - 1).…array

Get indices of a value in an array

Collect every index where a value appears.

function indices({ values, value }) { return values.flatMap((item, i) => (item === value ? [i] …array

Get the average of an array

Divide the sum by the count.

function average(values) { return values.reduce((sum, value) => sum + value, 0) / values.length…array

Intersperse element between elements

Insert a separator between every pair of items.

function intersperse({ values, separator }) { return values.flatMap((value, i) => (i === 0 ? [v…array

Sort an array of items by given key

Return a copy sorted by one field, ascending.

function sortBy({ values, key }) { return [...values].sort((a, b) => (a[key] > b[key] ? 1 : a[k…array

Check if an array is empty

Return true for an array without items.

function isEmptyArray(value) { return Array.isArray(value) && !value.length; }array

Get all nth items of an array

Keep every nth item, starting with the nth.

function getNthItems({ values, nth }) { return values.filter((_, i) => i % nth === nth - 1); }array

Merge two arrays

Concatenate two arrays, optionally dropping duplicates.

function merge({ a, b, unique = false }) { return unique ? [...new Set([...a, ...b])] : [...a, …array

Repeat an array

Concatenate an array with itself n times.

function repeatArray({ values, times }) { return Array(times).fill(values).flat(); }array

Empty an array

Remove every item in place, so all references see an empty array.

function empty(values) { values.length = 0; return values; }array

Partition an array based on a condition

Split items into [matching, not matching] in one pass.

function partition(values, predicate) { return values.reduce((groups, value) => (groups[predica…array

Shuffle an array

Return a new array in random order, every order equally likely.

function shuffle(values) { const result = [...values]; for (let i = result.length - 1; i > 0; i…array

Try a function in the local code lab, then read its limits before using it in an application.

Type two or more characters. Try “duplicate”, “webhook” or “retry”.