Functions

Browse all available utility functions by category

collection

countBy

Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the number of times the key was returned by iteratee.

Example:

countBy([6.1, 4.2, 6.3], Math.floor); // { '4': 1, '6': 2 }
collection

each

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection).

Example:

each([1, 2], (value) => console.log(value)); // Logs `1` then `2`
collection

eachRight

This method is like each except that it iterates over elements of collection from right to left.

Example:

eachRight([1, 2], (value) => console.log(value)); // Logs `2` then `1`
collection

every

Checks if predicate returns truthy for all elements of collection.

Example:

every([true, 1, null, 'yes'], Boolean); // false
collection

filter

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.

Example:

const users = [
collection

find

Iterates over elements of collection, returning the first element predicate returns truthy for.

Example:

const users = [
collection

findLast

This method is like find except that it iterates over elements of collection from right to left.

Example:

findLast([1, 2, 3, 4], (n) => n % 2 === 1); // 3
collection

flatMap

Creates a flattened array of values by running each element in collection thru iteratee and flattening the mapped results.

Example:

function duplicate(n: number) {
collection

flatMapDeep

This method is like flatMap except that it recursively flattens the mapped results.

Example:

function duplicate(n: number) {
collection

flatMapDepth

This method is like flatMap except that it recursively flattens the mapped results up to depth times.

Example:

function duplicate(n: number) {
collection

forEach

Iterates over elements of collection and invokes iteratee for each element.

Example:

forEach([1, 2], (value) => console.log(value)); // Logs `1` then `2`
collection

forEachRight

This method is like forEach except that it iterates over elements of collection from right to left.

Example:

forEachRight([1, 2], (value) => console.log(value)); // Logs `2` then `1`
collection

groupBy

Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection.

Example:

groupBy([6.1, 4.2, 6.3], Math.floor); // { '4': [4.2], '6': [6.1, 6.3] }
collection

includes

Checks if value is in collection.

Example:

includes([1, 2, 3], 1); // true
collection

invokeMap

Invokes the method at path of each element in collection, returning an array of the results of each invoked method.

Example:

invokeMap([[5, 1, 7], [3, 2, 1]], 'sort'); // [[1, 5, 7], [1, 2, 3]]
collection

keyBy

Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the last element responsible for generating the key.

Example:

const array = [
collection

map

Creates an array of values by running each element in collection thru iteratee.

Example:

function square(n: number) {
collection

orderBy

This method is like sortBy except that it allows specifying the sort orders of the iteratees to sort by.

Example:

const users = [
collection

partition

Creates an array of elements split into two groups, the first of which contains elements predicate returns truthy for, the second of which contains elements predicate returns falsey for.

Example:

const users = [
collection

reduce

Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee.

Example:

reduce([1, 2], (sum, n) => sum + n, 0); // 3
collection

reduceRight

This method is like reduce except that it iterates over elements of collection from right to left.

Example:

const array = [[0, 1], [2, 3], [4, 5]];
collection

reject

The opposite of filter; this method returns the elements of collection that predicate does not return truthy for.

Example:

const users = [
collection

sample

Gets a random element from collection.

Example:

sample([1, 2, 3, 4]); // 2 (random)
collection

sampleSize

Gets n random elements at unique keys from collection up to the size of collection.

Example:

sampleSize([1, 2, 3], 2); // [3, 1] (random)
collection

shuffle

Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.

Example:

shuffle([1, 2, 3, 4]); // [4, 1, 3, 2] (random)
collection

size

Gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.

Example:

size([1, 2, 3]); // 3
collection

some

Checks if predicate returns truthy for any element of collection.

Example:

some([null, 0, 'yes', false], Boolean); // true
collection

sortBy

Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee.

Example:

const users = [