Skip to content

Lodash - Array

Lodash offers a comprehensive set of functions specifically designed for array manipulation. While native JavaScript Array methods (.map, .filter, .reduce, .slice, .concat, etc.) cover many common use cases, Lodash provides additional utilities for tasks like splitting arrays into chunks, finding differences or intersections, advanced filtering, and working with sorted arrays.

This section highlights some unique and powerful array methods provided by Lodash.

Key Array Methods (Beyond Native Equivalents)

Section titled “Key Array Methods (Beyond Native Equivalents)”

Here are some Lodash array functions that offer functionality not directly available in standard JavaScript Array methods:

Method & SyntaxDescription
chunk
_.chunk(array, [size=1])
Creates an array of elements split into groups the length of size. If array can’t be split evenly, the final chunk will contain the remaining elements.
compact
_.compact(array)
Creates an array with all falsey values removed (false, null, 0, "", undefined, NaN).
difference
_.difference(array, [values])
Creates an array of array values not included in the other given arrays. Uses SameValueZero for equality comparisons.
differenceBy
.differenceBy(array, [values], [iteratee=.identity])
Like _.difference, but accepts an iteratee which is invoked for each element of array and values to generate the criterion by which they’re compared.
differenceWith
_.differenceWith(array, [values], [comparator])
Like _.difference, but accepts a comparator which is invoked to compare elements of array to values.
drop
_.drop(array, [n=1])
Creates a slice of array with n elements dropped from the beginning.
dropRight
_.dropRight(array, [n=1])
Creates a slice of array with n elements dropped from the end.
dropWhile
.dropWhile(array, [predicate=.identity])
Creates a slice of array excluding elements dropped from the beginning until predicate returns falsey.
flatten
_.flatten(array)
Flattens array a single level deep. Native equivalent: Array.prototype.flat() or array.flat(1).
flattenDeep
_.flattenDeep(array)
Recursively flattens array. Native equivalent: array.flat(Infinity).
flattenDepth
_.flattenDepth(array, [depth=1])
Recursively flattens array up to depth times. Native equivalent: array.flat(depth).
fromPairs
_.fromPairs(pairs)
Creates an object composed from key-value pairs (an array of arrays).
intersection
_.intersection([arrays])
Creates an array of unique values that are included in all given arrays. Uses SameValueZero for equality comparisons.
intersectionBy / intersectionWithLike _.intersection, but accept an iteratee or comparator.
nth
_.nth(array, [n=0])
Gets the element at index n of array. If n is negative, the nth element from the end is returned. Native equivalent: array.at(n).
pull
_.pull(array, [values])
Removes all given values from array. Mutates array.
pullAll
_.pullAll(array, values)
Like _.pull, but accepts an array of values to remove. Mutates array.
pullAllBy / pullAllWithLike _.pullAll, but accept an iteratee or comparator. Mutates array.
pullAt
_.pullAt(array, [indexes])
Removes elements from array corresponding to indexes and returns an array of removed elements. Mutates array.
remove
.remove(array, [predicate=.identity])
Removes all elements from array that predicate returns truthy for and returns an array of the removed elements. Mutates array.
sortedIndex
_.sortedIndex(array, value)
Uses a binary search to determine the lowest index at which value should be inserted into array in order to maintain its sort order.
sortedIndexBy / sortedIndexOf / sortedLastIndex / sortedLastIndexOfVariations for binary search based on iteratees or finding existing values.
sortedUniq
_.sortedUniq(array)
Optimized version of _.uniq for sorted arrays.
sortedUniqBy
_.sortedUniqBy(array, [iteratee])
Like _.sortedUniq, but accepts an iteratee.
take
_.take(array, [n=1])
Creates a slice of array with n elements taken from the beginning.
takeRight
_.takeRight(array, [n=1])
Creates a slice of array with n elements taken from the end.
takeWhile / takeRightWhileCreates slices taking elements while a predicate returns truthy.
union
_.union([arrays])
Creates an array of unique values, in order, from all given arrays.
unionBy / unionWithLike _.union, but accept an iteratee or comparator.
uniq
_.uniq(array)
Creates a duplicate-free version of an array. Uses SameValueZero for equality comparisons.
uniqBy / uniqWithLike _.uniq, but accept an iteratee or comparator.
unzip
_.unzip(array)
The inverse of _.zip; accepts an array of grouped elements and creates an array regrouping elements to their pre-zip configuration.
unzipWithLike _.unzip, but accepts an iteratee to specify how regrouped values should be combined.
without
_.without(array, [values])
Creates an array excluding all given values. Uses SameValueZero for equality comparisons.
xor
_.xor([arrays])
Creates an array of unique values that is the symmetric difference of the given arrays (elements that appear in an odd number of arrays).
xorBy / xorWithLike _.xor, but accept an iteratee or comparator.
zip
_.zip([arrays])
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, and so on.
zipObject
_.zipObject([props=[]], [values=[]])
Creates an object composed from arrays of property identifiers and value arrays.
zipObjectDeep
_.zipObjectDeep([props=[]], [values=[]])
Like _.zipObject, but supports property paths.
zipWith
.zipWith([arrays], [iteratee=.identity])
Like _.zip, but accepts an iteratee to specify how grouped values should be combined.
import _ from 'lodash-es';
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const primes = [2, 3, 5, 7];
const data = ['a', 'b', 'c', 'd', 'e'];
// Split numbers into chunks of 3
const chunks = _.chunk(numbers, 3);
// chunks: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
// Find numbers that are not prime
const nonPrimes = _.difference(numbers, primes);
// nonPrimes: [1, 4, 6, 8, 9, 10]
// Combine data with numbers using zip
const zippedData = _.zip(data, numbers);
// zippedData: [['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]]
// Note: Zip stops when the shortest array is exhausted.
// Create unique list from multiple arrays
const list1 = [1, 2, 3, 4];
const list2 = [3, 4, 5, 6];
const uniqueUnion = _.union(list1, list2);
// uniqueUnion: [1, 2, 3, 4, 5, 6]
console.log('Chunks:', chunks);
console.log('Non-Primes:', nonPrimes);
console.log('Zipped Data:', zippedData);
console.log('Union:', uniqueUnion);

These specialized array methods can significantly simplify complex array manipulations, making code more readable and maintainable compared to implementing the same logic using native methods alone.