Clone an array
Create a new array without copying the objects inside it.
function clone(values) { return [...values]; }arrayCollection · 50 utilities
Start with copies and equality, then move to grouping and aggregation. Notice where object identity differs from deep equality.
Create a new array without copying the objects inside it.
function clone(values) { return [...values]; }arrayKeep the first occurrence of each value, in its original order.
function unique(values) { return [...new Set(values)]; }arrayTurn a nested array into one sequence of values.
function flatten(values) { return values.flat(Infinity); }arrayBuild consecutive chunks without changing the original array.
function chunk({ values, size }) { return Array.from({ length: Math.ceil(values.length / size) …arrayReturn the distinct values that occur in both arrays.
function intersection({ left, right }) { const other = new Set(right); return [...new Set(left)…arraySort a numeric copy instead of using JavaScript’s default string ordering.
function sortNumbers(values) { return [...values].sort((a, b) => a - b); }arrayAdd numeric values with an explicit result for an empty array.
function sum(values) { return values.reduce((total, value) => total + value, 0); }arrayCollect 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…arrayWrap a single value in an array and return arrays unchanged.
function castArray(value) { return Array.isArray(value) ? value : [value]; }arrayMap every string with Number.
function toNumbers(values) { return values.map(Number); }arrayBuild the power set, smallest subsets first.
function getSubsets(values) { return values.reduce((subsets, value) => subsets.concat(subsets.m…arrayMerge arrays and keep each value once, in first-seen order.
function union(arrays) { return [...new Set(arrays.flat())]; }arrayTranspose a rectangular matrix.
function transpose(matrix) { return matrix[0].map((_, column) => matrix.map(row => row[column])…arrayReturn a copy with the items at i and j exchanged.
function swapItems({ values, i, j }) { const copy = [...values]; [copy[i], copy[j]] = [copy[j],…arrayRegroup 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…arrayCheck 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…arrayIndex records by a key field.
function toObject({ values, key }) { return Object.fromEntries(values.map(item => [item[key], i…arrayCount items strictly equal to a value.
function countOccurrences({ values, value }) { return values.filter(item => item === value).len…arrayTally every distinct value into an object.
function countEach(values) { return { ...values.reduce((counts, value) => ((counts[value] = (co…arrayList the integers from min to max inclusive.
function range({ min, max }) { return Array.from({ length: max - min + 1 }, (_, i) => min + i);…arrayCombine one item from each set in every possible way.
function cartesian(sets) { return sets.reduce((product, set) => product.flatMap(prefix => set.m…arraySpread the numbers into Math.min.
function min(values) { return Math.min(...values); }arrayRank each number by how many values are larger, starting at 1.
function ranking(values) { return values.map(value => values.filter(other => other > value).len…arrayFilter with Boolean.
function removeFalsy(values) { return values.filter(Boolean); }arrayPair the n-th items of several arrays.
function zip(arrays) { return Array.from({ length: Math.max(0, ...arrays.map(array => array.len…arrayCompare the sorted sets of distinct values.
function isEqualIgnoringOrder({ a, b }) { const normalize = values => JSON.stringify([...new Se…arrayTally records by the value of one field.
function countBy({ values, key }) { return { ...values.reduce((counts, item) => ((counts[item[k…arrayReplace each number with the running total up to it.
function accumulate(values) { let total = 0; return values.map(value => (total += value)); }arrayReturn the index of the first largest number.
function indexOfMax(values) { return values.reduce((best, value, i) => (value > values[best] ? …arrayReturn the record with the largest value in a field.
function maxBy({ values, key }) { return values.reduce((best, item) => (best === null || item[k…arraySpread the numbers into Math.max.
function max(values) { return Math.max(...values); }arrayList the first count lowercase ASCII letters.
function alphabet(count) { return Array.from({ length: count }, (_, i) => String.fromCharCode(9…arraySlide a window of a given size across an array.
function windows({ values, size }) { return size > values.length ? [] : values.slice(size - 1).…arrayCollect every index where a value appears.
function indices({ values, value }) { return values.flatMap((item, i) => (item === value ? [i] …arrayDivide the sum by the count.
function average(values) { return values.reduce((sum, value) => sum + value, 0) / values.length…arrayKeep the first occurrence of each value with a Set.
function uniqueValues(values) { return [...new Set(values)]; }arrayInsert a separator between every pair of items.
function intersperse({ values, separator }) { return values.flatMap((value, i) => (i === 0 ? [v…arrayReturn a copy sorted by one field, ascending.
function sortBy({ values, key }) { return [...values].sort((a, b) => (a[key] > b[key] ? 1 : a[k…arrayReturn true for an array without items.
function isEmptyArray(value) { return Array.isArray(value) && !value.length; }arrayReturn the item nearest to a target.
function closest({ values, target }) { return values.reduce((best, value) => (Math.abs(value - …arrayReturn the index of the first smallest number.
function indexOfMin(values) { return values.reduce((best, value, i) => (value < values[best] ? …arrayMap strings to lengths and take the maximum.
function longestLength(words) { return Math.max(0, ...words.map(word => word.length)); }arrayReturn the record with the smallest value in a field.
function minBy({ values, key }) { return values.reduce((best, item) => (best === null || item[k…arrayKeep every nth item, starting with the nth.
function getNthItems({ values, nth }) { return values.filter((_, i) => i % nth === nth - 1); }arrayConcatenate two arrays, optionally dropping duplicates.
function merge({ a, b, unique = false }) { return unique ? [...new Set([...a, ...b])] : [...a, …arrayConcatenate an array with itself n times.
function repeatArray({ values, times }) { return Array(times).fill(values).flat(); }arrayRemove every item in place, so all references see an empty array.
function empty(values) { values.length = 0; return values; }arrayScan from the end and return the first index whose item matches.
function lastIndex(values, predicate) { return values.findLastIndex(predicate); }arraySplit items into [matching, not matching] in one pass.
function partition(values, predicate) { return values.reduce((groups, value) => (groups[predica…arrayReturn 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…arrayTry a function in the local code lab, then read its limits before using it in an application.