Functions for working with arrays and array-like objects. These utilities provide efficient array manipulation, transformation, and processing capabilities.
| Function | Description | Parameters | Returns |
|---|---|---|---|
chunk |
Splits array into chunks of specified size | array, size |
Array of chunks |
compact |
Removes falsy values from array | array |
New array without falsy values |
difference |
Array values not in other arrays | array, ...values |
New array of differences |
intersection |
Common values in all arrays | ...arrays |
New array of intersections |
union |
Unique values from all arrays | ...arrays |
New array of unique values |
flatten |
Flattens array one level deep | array |
New flattened array |
flattenDeep |
Recursively flattens array | array |
New deeply flattened array |
head |
Gets first element of array | array |
First element or undefined |
tail |
Gets all but first element | array |
New array without first element |
last |
Gets last element of array | array |
Last element or undefined |
initial |
Gets all but last element | array |
New array without last element |
take |
Takes first n elements | array, n? |
New array of taken elements |
takeRight |
Takes last n elements | array, n? |
New array of taken elements |
drop |
Drops first n elements | array, n? |
New array without dropped elements |
dropRight |
Drops last n elements | array, n? |
New array without dropped elements |
sortBy |
Sorts array by iteratees | collection, ...iteratees |
New sorted array |
zip |
Groups elements by index | ...arrays |
New array of grouped elements |
unzip |
Unzips grouped elements | array |
New array of unzipped elements |
pull |
Removes given values (mutable) | array, ...values |
Modified array |
remove |
Removes elements by predicate | array, predicate |
New array of removed elements |
without |
Array without given values | array, ...values |
New array without values |
Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
array(Array): The array to processsize(number): The length of each chunk (default: 1)
New array of chunks
import { chunk } from 'highdash';
// Basic chunking
console.log(chunk(['a', 'b', 'c', 'd'], 2));
// => [['a', 'b'], ['c', 'd']]
console.log(chunk(['a', 'b', 'c', 'd'], 3));
// => [['a', 'b', 'c'], ['d']]
// Uneven chunks
console.log(chunk([1, 2, 3, 4, 5], 2));
// => [[1, 2], [3, 4], [5]]
// Processing data in batches
const data = Array.from({ length: 100 }, (_, i) => i);
const batches = chunk(data, 10);
console.log(batches.length); // 10 batches of 10 items eachCreates an array with all falsy values removed. The values false, null, 0, "", undefined, and NaN are falsy.
array(Array): The array to compact
New array of filtered values
import { compact } from 'highdash';
// Remove falsy values
console.log(compact([0, 1, false, 2, '', 3]));
// => [1, 2, 3]
console.log(compact([null, undefined, 0, false, '', NaN, 1, 2]));
// => [1, 2]
// With mixed types
console.log(compact(['hello', '', 'world', null, 42, 0]));
// => ['hello', 'world', 42]Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons.
array(Array): The array to inspect...values(Array[]): The values to exclude
New array of filtered values
import { difference } from 'highdash';
// Basic difference
console.log(difference([2, 1], [2, 3]));
// => [1]
// Multiple exclusions
console.log(difference([1, 2, 3, 4, 5], [2, 4], [3]));
// => [1, 5]
// With objects (uses reference equality)
const obj1 = { id: 1 };
const obj2 = { id: 2 };
const obj3 = { id: 3 };
console.log(difference([obj1, obj2, obj3], [obj2]));
// => [obj1, obj3]Creates an array of unique values that are included in all given arrays using SameValueZero for equality comparisons.
...arrays(Array[]): The arrays to inspect
New array of intersecting values
import { intersection } from 'highdash';
// Basic intersection
console.log(intersection([2, 1], [2, 3]));
// => [2]
// Multiple arrays
console.log(intersection([1, 2, 3], [2, 3, 4], [3, 4, 5]));
// => [3]
// With strings
console.log(intersection(['a', 'b', 'c'], ['b', 'c', 'd'], ['c', 'd', 'e']));
// => ['c']Creates an array of unique values, in order, from all given arrays using SameValueZero for equality comparisons.
...arrays(Array[]): The arrays to inspect
New array of combined unique values
import { union } from 'highdash';
// Basic union
console.log(union([2], [1, 2]));
// => [2, 1]
// Multiple arrays
console.log(union([1, 2], [2, 3], [3, 4]));
// => [1, 2, 3, 4]
// With strings
console.log(union(['a', 'b'], ['b', 'c'], ['c', 'd']));
// => ['a', 'b', 'c', 'd']Flattens array a single level deep.
array(Array): The array to flatten
New flattened array
import { flatten } from 'highdash';
// Single level flattening
console.log(flatten([1, [2, [3, [4]], 5]]));
// => [1, 2, [3, [4]], 5]
console.log(flatten([[1, 2], [3, 4], [5, 6]]));
// => [1, 2, 3, 4, 5, 6]
// Mixed types
console.log(flatten(['a', ['b', 'c'], 'd']));
// => ['a', 'b', 'c', 'd']Recursively flattens array.
array(Array): The array to flatten
New deeply flattened array
import { flattenDeep } from 'highdash';
// Deep flattening
console.log(flattenDeep([1, [2, [3, [4]], 5]]));
// => [1, 2, 3, 4, 5]
console.log(flattenDeep([[1, 2], [[3, 4]], [5, 6]]));
// => [1, 2, 3, 4, 5, 6]
// Complex nested structure
console.log(flattenDeep([1, [2, [3, [4, [5]]]]]));
// => [1, 2, 3, 4, 5]Gets the first element of array.
array(Array): The array to query
First element or undefined
import { head } from 'highdash';
console.log(head([1, 2, 3]));
// => 1
console.log(head([]));
// => undefined
console.log(head(['a', 'b', 'c']));
// => 'a'Gets all but the first element of array.
array(Array): The array to query
New array without first element
import { tail } from 'highdash';
console.log(tail([1, 2, 3]));
// => [2, 3]
console.log(tail([1]));
// => []
console.log(tail([]));
// => []Gets the last element of array.
array(Array): The array to query
Last element or undefined
import { last } from 'highdash';
console.log(last([1, 2, 3]));
// => 3
console.log(last([]));
// => undefined
console.log(last(['a', 'b', 'c']));
// => 'c'Gets all but the last element of array.
array(Array): The array to query
New array without last element
import { initial } from 'highdash';
console.log(initial([1, 2, 3]));
// => [1, 2]
console.log(initial([1]));
// => []
console.log(initial([]));
// => []Creates a slice of array with n elements taken from the beginning.
array(Array): The array to queryn(number): The number of elements to take (default: 1)
New array of taken elements
import { take } from 'highdash';
console.log(take([1, 2, 3]));
// => [1]
console.log(take([1, 2, 3], 2));
// => [1, 2]
console.log(take([1, 2, 3], 5));
// => [1, 2, 3]
console.log(take([1, 2, 3], 0));
// => []Creates a slice of array with n elements taken from the end.
array(Array): The array to queryn(number): The number of elements to take (default: 1)
New array of taken elements
import { takeRight } from 'highdash';
console.log(takeRight([1, 2, 3]));
// => [3]
console.log(takeRight([1, 2, 3], 2));
// => [2, 3]
console.log(takeRight([1, 2, 3], 5));
// => [1, 2, 3]
console.log(takeRight([1, 2, 3], 0));
// => []Creates a slice of array with n elements dropped from the beginning.
array(Array): The array to queryn(number): The number of elements to drop (default: 1)
New array without dropped elements
import { drop } from 'highdash';
console.log(drop([1, 2, 3]));
// => [2, 3]
console.log(drop([1, 2, 3], 2));
// => [3]
console.log(drop([1, 2, 3], 5));
// => []
console.log(drop([1, 2, 3], 0));
// => [1, 2, 3]Creates a slice of array with n elements dropped from the end.
array(Array): The array to queryn(number): The number of elements to drop (default: 1)
New array without dropped elements
import { dropRight } from 'highdash';
console.log(dropRight([1, 2, 3]));
// => [1, 2]
console.log(dropRight([1, 2, 3], 2));
// => [1]
console.log(dropRight([1, 2, 3], 5));
// => []
console.log(dropRight([1, 2, 3], 0));
// => [1, 2, 3]Creates an array of elements, sorted in ascending order by the results of running each element through iteratees. This method performs a stable sort.
collection(Array|Object): The collection to iterate over...iteratees(Function|string): The iteratees to sort by
New sorted array
import { sortBy } from 'highdash';
const users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 34 }
];
// Sort by single property
console.log(sortBy(users, 'age'));
// => [{ user: 'barney', age: 34 }, { user: 'barney', age: 36 }, ...]
// Sort by multiple properties
console.log(sortBy(users, ['user', 'age']));
// => [{ user: 'barney', age: 34 }, { user: 'barney', age: 36 }, ...]
// Sort by function
console.log(sortBy(users, user => user.age));
// => [{ user: 'barney', age: 34 }, { user: 'barney', age: 36 }, ...]
// Sort numbers
console.log(sortBy([3, 1, 4, 1, 5]));
// => [1, 1, 3, 4, 5]Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
...arrays(Array[]): The arrays to process
New array of grouped elements
import { zip } from 'highdash';
console.log(zip(['a', 'b'], [1, 2], [true, false]));
// => [['a', 1, true], ['b', 2, false]]
console.log(zip(['a', 'b', 'c'], [1, 2]));
// => [['a', 1], ['b', 2], ['c', undefined]]
// Transpose matrix
const matrix = [[1, 2, 3], [4, 5, 6]];
console.log(zip(...matrix));
// => [[1, 4], [2, 5], [3, 6]]This method is like zip except that it accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
array(Array): The array of grouped elements to process
New array of regrouped elements
import { unzip } from 'highdash';
const zipped = [['a', 1, true], ['b', 2, false]];
console.log(unzip(zipped));
// => [['a', 'b'], [1, 2], [true, false]]
// Unzip matrix
const matrix = [[1, 4], [2, 5], [3, 6]];
console.log(unzip(matrix));
// => [[1, 2, 3], [4, 5, 6]]Removes all given values from array using SameValueZero for equality comparisons. This function mutates the original array.
array(Array): The array to modify...values(any[]): The values to remove
Modified array
import { pull } from 'highdash';
const array = ['a', 'b', 'c', 'a', 'b', 'c'];
pull(array, 'a', 'c');
console.log(array);
// => ['b', 'b']
const numbers = [1, 2, 3, 1, 2, 3];
pull(numbers, 1, 3);
console.log(numbers);
// => [2, 2]Removes all elements from array that predicate returns truthy for and returns an array of the removed elements. This function mutates the original array.
array(Array): The array to modifypredicate(Function): The function invoked per iteration
New array of removed elements
import { remove } from 'highdash';
const array = [1, 2, 3, 4];
const evens = remove(array, n => n % 2 === 0);
console.log(array);
// => [1, 3]
console.log(evens);
// => [2, 4]
const users = [
{ name: 'Alice', active: true },
{ name: 'Bob', active: false },
{ name: 'Charlie', active: true }
];
const inactive = remove(users, user => !user.active);
console.log(users);
// => [{ name: 'Alice', active: true }, { name: 'Charlie', active: true }]
console.log(inactive);
// => [{ name: 'Bob', active: false }]Creates an array excluding all given values using SameValueZero for equality comparisons.
array(Array): The array to inspect...values(any[]): The values to exclude
New array without excluded values
import { without } from 'highdash';
console.log(without([2, 1, 2, 3], 1, 2));
// => [3]
console.log(without(['a', 'b', 'c', 'a', 'b'], 'a', 'b'));
// => ['c']
// With objects (uses reference equality)
const obj1 = { id: 1 };
const obj2 = { id: 2 };
const obj3 = { id: 3 };
console.log(without([obj1, obj2, obj3], obj2));
// => [obj1, obj3]// Import specific functions
import { chunk, compact, flatten } from 'highdash';
// Import from specific module (better tree-shaking)
import { chunk } from 'highdash/array/chunk.js';
import { compact } from 'highdash/array/compact.js';
import { flatten } from 'highdash/array/flatten.js';
// Import all array functions
import * as array from 'highdash/array';flattenDeep: 2.1× faster than Lodash - uses nativeArray.flat(Infinity)- Native methods: Uses native
Array.flat()andArray.filter()where possible - Efficient algorithms: Optimized implementations for common operations
- Memory usage: Creates new arrays instead of mutating (except
pullandremove) - Type safety: Full TypeScript support with proper type inference
Performance metrics based on Node.js 18+ benchmarks, averaged over 5 runs.
- Data processing: Use
chunkfor batch processing - Data cleaning: Use
compactto remove falsy values - Array manipulation: Use
take,drop,head,tailfor slicing - Set operations: Use
union,intersection,differencefor array math - Sorting: Use
sortByfor complex sorting operations - Matrix operations: Use
zipandunzipfor transposition