Skip to main content

Collection methods

Explore the list of utility methods for working with collections.


The collectionUtils library provides various utility functions for working with collections.

Info

collectionUtils library uses Lodash's Collection methods. For full details of how to use these methods, refer to Lodash documentation.

Overview

The collectionUtils library offers a diverse range of array manipulation methods. Here's the list of some of the most common methods:

Iteration and iterators

  • each: Iterates over elements in a collection, invoking an iteratee function for each element.
  • forEach: Iterates over elements and applies an iteratee function.

Filtering and validation

  • every: Checks if all elements in the collection satisfy a given predicate function.
  • filter: Creates a new collection with elements that pass the given predicate function.
  • reject: Creates a new collection with elements that do not satisfy the given predicate function.
  • some: Checks if at least one element in the collection satisfies a given predicate function.

Finding and locating

  • find: Returns the first element in the collection that satisfies a provided predicate function.
  • findLast: Similar to find, but searches for the last satisfying element.
  • includes: Checks if the collection includes a specific value, optionally starting from a given index.

Transformation and mapping

  • countBy: Groups and counts collection elements based on the result of an iteratee.
  • flatMap: Maps each element to a collection and then flattens the result.
  • invokeMap: Invokes a method on each element and returns the results.
  • keyBy: Creates an object where keys are the results of an iteratee and values are corresponding elements.
  • map: Transforms each element using an iteratee function.
  • sortBy: Sorts the collection elements based on the iteratees' results.

Grouping and partitioning

  • groupBy: Groups collection elements based on the results of an iteratee.
  • partition: Partitions elements into two arrays based on whether they satisfy a given predicate function.

Ordering and randomization

  • orderBy: Sorts the collection based on the results of iteratees, with support for custom orders.
  • shuffle: Shuffles the elements in the collection.

Methods reference in collectionUtils library

collectionUtils.countBy(collection, iteratee)

Creates an object composed of keys generated from the results of running each element of the collection through the iteratee function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Object): The composed object.

Example

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

collectionUtils.each(collection, iteratee)

Iterates over elements of a collection, invoking the iteratee function for each element.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Array|Object): The original collection.

Example

const collection = [1, 2, 3];
collectionUtils.each(collection, item => console.log(item));
// Result: Logs 1, 2, 3

collectionUtils.eachRight(collection, iteratee)

This method is like collectionUtils.each except that it iterates over elements in reverse.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Array|Object): The original collection.

Example

const collection = [1, 2, 3];
collectionUtils.eachRight(collection, item => console.log(item));
// Result: Logs 3, 2, 1

collectionUtils.every(collection, predicate)

Checks if all elements of a collection pass a truthy test.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • predicate (Function): The predicate function.

Returns

  • (boolean): true if all elements pass the predicate, false otherwise.

Example

const collection = [true, 1, null, 'yes'];
const result = collectionUtils.every(collection, Boolean);
// Result: false

collectionUtils.filter(collection, predicate)

Filters elements of a collection based on the predicate function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • predicate (Function): The predicate function.

Returns

  • (Array): The filtered array.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.filter(collection, item => item % 2 === 0);
// Result: [2, 4]

collectionUtils.find(collection, predicate)

Finds the first element of a collection that passes the predicate function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • predicate (Function): The predicate function.

Returns

  • (*|undefined): The found element, or undefined if not found.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.find(collection, item => item > 2);
// Result: 3

collectionUtils.findLast(collection, predicate)

This method is like collectionUtils.find except that it iterates over elements in reverse.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • predicate (Function): The predicate function.

Returns

  • (*|undefined): The found element, or undefined if not found.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.findLast(collection, item => item > 2);
// Result: 4

collectionUtils.flatMap(collection, iteratee)

Creates a flattened array by running each element of a collection through the iteratee function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Array): The flattened array.

Example

const collection = [1, 2, 3];
const result = collectionUtils.flatMap(collection, item => [item, item * 2]);
// Result: [1, 2, 2, 4, 3, 6]

collectionUtils.flatMapDeep(collection, iteratee)

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

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Array): The deeply flattened array.

Example

const collection = [1, 2, 3];
const result = collectionUtils.flatMapDeep(collection, item => [[item, item], [item * 2]]);
// Result: [1, 1, 2, 2, 3, 3, 2, 4, 6]

collectionUtils.flatMapDepth(collection, iteratee, depth)

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

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.
  • depth (number): The maximum recursion depth.

Returns

  • (Array): The flattened array up to the specified depth.

Example

const collection = [1, 2, 3];
const result = collectionUtils.flatMapDepth(collection, item => [[item, item], [item * 2]], 1);
// Result: [1, 1, 2, 2, 3, 3, 2, 4, 6]

collectionUtils.forEach(collection, iteratee)

Iterates over elements of a collection, invoking the iteratee function for each element.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Array|Object): The original collection.

Example

const collection = [1, 2, 3];
collectionUtils.forEach(collection, item => console.log(item));
// Result: Logs 1, 2, 3

collectionUtils.forEachRight(collection, iteratee)

This method is like collectionUtils.forEach except that it iterates over elements in reverse.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Array|Object): The original collection.

Example

const collection = [1, 2, 3];
collectionUtils.forEachRight(collection, item => console.log(item));
// Result: Logs 3, 2, 1

collectionUtils.groupBy(collection, iteratee)

Creates an object composed of keys generated from the results of running each element of the collection through the iteratee function. The corresponding value of each key is an array of elements that correspond to the key.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Object): The composed object.

Example

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

collectionUtils.includes(collection, value, fromIndex)

Checks if a value is present in a collection using strict equality.

Parameters

  • collection (Array|Object): The collection to search.
  • value (*): The value to search for.
  • fromIndex (number): The index to start searching from.

Returns

  • (boolean): true if the value is found, false otherwise.

Example

const collection = [1, 2, 3];
const result = collectionUtils.includes(collection, 2);
// Result: true

collectionUtils.invokeMap(collection, path, ...args)

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

Parameters

  • collection (Array|Object): The collection to iterate over.
  • path (string|Array|Function): The path of the method to invoke.
  • ...args (*): The arguments to invoke the method with.

Returns

  • (Array): The array of results.

Example

const collection = [[5, 1, 7], [3, 2, 1]];
const result = collectionUtils.invokeMap(collection, 'sort');
// Result: [[1, 5, 7], [1, 2, 3]]

collectionUtils.keyBy(collection, iteratee)

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

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Object): The composed object.

Example

const collection = [
{ id: 'one', value: 1 },
{ id: 'two', value: 2 },
{ id: 'three', value: 3 }
];
const result = collectionUtils.keyBy(collection, 'id');
// Result: { 'one': { id: 'one', value: 1 }, 'two': { id: 'two', value: 2 }, 'three': { id: 'three', value: 3 } }

collectionUtils.map(collection, iteratee)

Creates an array of values by running each element of the collection through the iteratee function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.

Returns

  • (Array): The mapped array.

Example

const collection = [1, 2, 3];
const result = collectionUtils.map(collection, item => item * 2);
// Result: [2, 4, 6]

collectionUtils.orderBy(collection, iteratees, orders)

Creates an array of elements that is sorted in ascending order by the results of running each element of the collection through the iteratees.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratees (Array|Function|string): The iteratees to sort by.
  • orders (Array|string): The sort orders.

Returns

  • (Array): The sorted array.

Example

const collection = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Bob', age: 35 }
];
const result = collectionUtils.orderBy(collection, ['name', 'age'], ['asc', 'desc']);
// Result: [{ name: 'Bob', age: 35 }, { name: 'Jane', age: 25 }, { name: 'John', age: 30 }]

collectionUtils.partition(collection, predicate)

Creates an array of elements split into two groups based on the results of running each element of the collection through the predicate function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • predicate (Function): The predicate function.

Returns

  • (Array): The partitioned array.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.partition(collection, item => item % 2 === 0);
// Result: [[2, 4], [1, 3]]

collectionUtils.reduce(collection, iteratee, accumulator)

Reduces a collection to a single value by iterating over elements and applying an iteratee function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function.
  • accumulator (*): The initial accumulator value.

Returns

  • (*): The reduced value.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.reduce(collection, (sum, num) => sum + num, 0);
// Result: 10

collectionUtils.reduceRight(collection, iteratee, accumulator)

This method is like collectionUtils.reduce except that it iterates over elements in reverse.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The iteratee function. accumulator (*): The initial accumulator value.

Returns

  • (*): The reduced value.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.reduceRight(collection, (sum, num) => sum + num, 0);
// Result: 10

collectionUtils.reject(collection, predicate)

The opposite of collectionUtils.filter. Rejects elements of a collection based on the predicate function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • predicate (Function): The predicate function.

Returns

  • (Array): The rejected array.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.reject(collection, item => item % 2 === 0);
// Result: [1, 3]

collectionUtils.sample(collection)

Gets a random element from a collection.

Parameters

  • collection (Array|Object): The collection to sample from.

Returns

  • (*): The randomly selected element.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.sample(collection);
// Result: Random element from the collection

collectionUtils.sampleSize(collection, n)

Gets n random elements from a collection.

Parameters

  • collection (Array|Object): The collection to sample from.
  • n (number): The number of elements to sample.

Returns

  • (Array): The array of randomly selected elements.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.sampleSize(collection, 2);
// Result: Array of 2 randomly selected elements from the collection

collectionUtils.shuffle(collection)

Creates a shuffled copy of a collection.

Parameters

  • collection (Array|Object): The collection to shuffle.

Returns

  • (Array): The shuffled array.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.shuffle(collection);
// Result: Shuffled array of elements from the collection

collectionUtils.size(collection)

Gets the size of a collection.

Parameters

  • collection (Array|Object): The collection to get the size of.

Returns

  • (number): The size of the collection.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.size(collection);
// Result: 4

collectionUtils.some(collection, predicate)

Checks if any element of a collection passes the predicate function.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • predicate (Function): The predicate function.

Returns

  • (boolean): true if any element passes the predicate, false otherwise.

Example

const collection = [1, 2, 3, 4];
const result = collectionUtils.some(collection, item => item % 2 === 0);
// Result: true

collectionUtils.sortBy(collection, iteratees)

Creates an array of elements that is sorted in ascending order by the results of running each element of the collection through the iteratees.

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratees (Array|Function|string): The iteratees to sort by.

Returns

  • (Array): The sorted array.

Example

const collection = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Bob', age: 35 }
];
const result = collectionUtils.sortBy(collection, ['age']);
// Result: [{ name: 'Jane', age: 25 }, { name: 'John', age: 30 }, { name: 'Bob', age: 35 }]