Array Functions
A collection of utility functions for working with arrays.
chunk
Splits an array into chunks of the specified size.
const array = [1, 2, 3, 4, 5, 6, 7, 8];const chunked = chunk(array, 3);console.log(chunked); // [[1, 2, 3], [4, 5, 6], [7, 8]]/** * Splits an array into chunks of the specified size * @param array - The array to chunk * @param size - The size of each chunk * @returns An array of chunks */function chunk<T>(array: T[], size: number): T[][] { if (!array.length || size <= 0) { return []; }
const chunks: T[][] = []; for (let i = 0; i < array.length; i += size) { chunks.push(array.slice(i, i + size)); }
return chunks;}uniq
Removes duplicate values from an array.
const array = [1, 2, 2, 3, 4, 4, 5];const uniqueValues = uniq(array);console.log(uniqueValues); // [1, 2, 3, 4, 5]/** * Removes duplicate values from an array * @param array - The array to process * @returns A new array with unique values */function uniq<T>(array: T[]): T[] { return [...new Set(array)];}uniqBy
Removes duplicate values from an array based on a computed key.
const array = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 1, name: 'John (duplicate)' }];
// Creating a unique list based on the id propertyconst uniqueById = uniqBy(array, item => item.id);console.log(uniqueById);// [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
// For simple values, you can use a mapping functionconst numbers = [1.1, 2.5, 1.9, 2.1];const uniqueByFloor = uniqBy(numbers, Math.floor);console.log(uniqueByFloor); // [1.1, 2.5]/** * Removes duplicate values from an array based on a key function * @param array - The array to process * @param iteratee - Function invoked per element to generate the criterion for uniqueness * @returns A new array with unique values determined by the iteratee */function uniqBy<T, K>(array: T[], iteratee: (item: T) => K): T[] { const seen = new Map<K, boolean>(); return array.filter(item => { const key = iteratee(item); if (seen.has(key)) { return false; } seen.set(key, true); return true; });}groupBy
Groups array items based on a key function.
const people = [ { name: 'John', age: 25 }, { name: 'Jane', age: 30 }, { name: 'Jack', age: 25 }];
const grouped = groupBy(people, person => person.age);console.log(grouped);// {// '25': [{ name: 'John', age: 25 }, { name: 'Jack', age: 25 }],// '30': [{ name: 'Jane', age: 30 }]// }/** * Groups array items based on a key function * @param array - The array to group * @param keyFn - Function that returns the grouping key for an item * @returns An object with keys from keyFn and values as arrays of matching items */function groupBy<T, K extends string | number | symbol>( array: T[], keyFn: (item: T) => K): Record<K, T[]> { return array.reduce((result, item) => { const key = keyFn(item); if (!result[key]) { result[key] = []; } result[key].push(item); return result; }, {} as Record<K, T[]>);}head
Gets the first element of an array.
const array = [1, 2, 3, 4, 5];const first = head(array);console.log(first); // 1
const emptyArray = [];const emptyResult = head(emptyArray);console.log(emptyResult); // undefined/** * Gets the first element of an array * @param array - The array to query * @returns The first element of the array, or undefined if the array is empty */function head<T>(array: T[]): T | undefined { return array[0];}last
Gets the last element of an array.
const array = [1, 2, 3, 4, 5];const lastElement = last(array);console.log(lastElement); // 5
const emptyArray = [];const emptyResult = last(emptyArray);console.log(emptyResult); // undefined/** * Gets the last element of an array * @param array - The array to query * @returns The last element of the array, or undefined if the array is empty */function last<T>(array: T[]): T | undefined { return array.length ? array[array.length - 1] : undefined;}