Skip to main content

Array methods

Discover array manipulation methods in Actioner with examples.


This documentation provides the methods available in the arrayUtils library, a go-to resource for a comprehensive collection of array manipulation methods.

Info

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

Overview

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

Partitioning and grouping

  • chunk: Divides an array into chunks of a specified size.

Refining and filtering

  • compact: Removes falsey values (false, null, 0, "", undefined, and NaN) from an array.
  • difference: Creates an array of values not present in the other given arrays.
  • remove: Removes elements from an array based on a provided predicate function.

Segmenting and extracting

  • drop: Creates a new array with the first n elements removed.
  • dropRight: Creates a new array with the last n elements removed.
  • slice: Creates a slice of an array from the start index up to, but not including, the end index.

Populating and replacing

  • fill: Fills elements of an array with the specified value within the specified range.

Querying and locating

  • findIndex: Returns the index of the first element in the array that satisfies a provided predicate function.

Flattening and unfolding

  • flatten: Flattens a nested array structure into a single array.

Indexing and positioning

  • indexOf: Returns the first index at which a given value can be found in the array, optionally starting from a specific index.

Accessing and retrieving

  • last: Returns the last element of the array.

Mutating and rearranging

  • pull: Removes all given values from the array.
  • reverse: Reverses the elements in an array.

Methods reference in arrayUtils library

arrayUtils.chunk(array, size)

Creates an array of elements split into groups the length of size.

Parameters

  • array (Array): The source array to be chunked.
  • size (number): The size of each chunk.

Returns

  • (Array): A new array of chunks.

Example

const sourceArray = [1, 2, 3, 4, 5, 6, 7];
const chunked = arrayUtils.chunk(sourceArray, 3);
// Result: [[1, 2, 3], [4, 5, 6], [7]]

arrayUtils.compact(array)

Creates an array with all falsey values removed.

Parameters

  • array (Array): The source array to be compacted.

Returns

  • (Array): A new array with falsey values removed.

Example

const sourceArray = [0, 1, false, 2, '', 3];
const compacted = arrayUtils.compact(sourceArray);
// Result: [1, 2, 3]

arrayUtils.concat(arrays)

Creates a new array concatenating source arrays.

Parameters

  • arrays (...Array): The arrays to concatenate.

Returns

  • (Array): The concatenated array.

Example

const array1 = [1, 2];
const array2 = [3, 4];
const concatenated = arrayUtils.concat(array1, array2);
// Result: [1, 2, 3, 4]

arrayUtils.difference(array, values)

Creates an array of values not included in the other given arrays.

Parameters

  • array (Array): The source array to compare.
  • values (Array): The values to exclude.

Returns

  • (Array): A new array of differing values.

Example

const sourceArray = [2, 1, 5];
const valuesToExclude = [2, 3];
const difference = arrayUtils.difference(sourceArray, valuesToExclude);
// Result: [1, 5]

arrayUtils.differenceBy(array, values, iteratee)

Creates an array of values not included in the other given arrays, determined by an iteratee.

Parameters

  • array (Array): The source array to compare.
  • values (Array): The values to exclude.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (Array): A new array of differing values.

Example

const sourceArray = [{ 'x': 2 }, { 'x': 1 }];
const valuesToExclude = [{ 'x': 1 }];
const difference = arrayUtils.differenceBy(sourceArray, valuesToExclude, 'x');
// Result: [{ 'x': 2 }]

arrayUtils.differenceWith(array, values, comparator)

Creates an array of values not included in the other given arrays, determined by a custom comparator.

Parameters

  • array (Array): The source array to compare.
  • values (Array): The values to exclude.
  • comparator (Function): The comparator function to determine values.

Returns

  • (Array): A new array of differing values.

Example

const sourceArray = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
const valuesToExclude = [{ 'x': 1, 'y': 2 }];
const difference = arrayUtils.differenceWith(sourceArray, valuesToExclude, (a, b) => a.x === b.x && a.y === b.y);
// Result: [{ 'x': 2, 'y': 1 }]

arrayUtils.drop(array, n)

Creates a new array with n elements dropped from the beginning.

Parameters

  • array (Array): The source array to drop elements from.
  • n (number): The number of elements to drop.

Returns

  • (Array): A new array with elements dropped.

Example

const sourceArray = [1, 2, 3, 4, 5];
const dropped = arrayUtils.drop(sourceArray, 2);
// Result: [3, 4, 5]

arrayUtils.dropRight(array, n)

Creates a new array with n elements dropped from the end.

Parameters

  • array (Array): The source array to drop elements from.
  • n (number): The number of elements to drop.

Returns

  • (Array): A new array with elements dropped.

Example

const sourceArray = [1, 2, 3, 4, 5];
const droppedRight = arrayUtils.dropRight(sourceArray, 2);
// Result: [1, 2, 3]

arrayUtils.dropRightWhile(array, predicate)

Creates a new array with elements dropped from the end until the predicate returns falsy.

Parameters

  • array (Array): The source array to drop elements from.
  • predicate (Function): The function invoked per iteration.

Returns

  • (Array): A new array with elements dropped.

Example

const sourceArray = [1, 2, 3, 4, 5];
const droppedRightWhile = arrayUtils.dropRightWhile(sourceArray, num => num > 2);
// Result: [1, 2]

arrayUtils.dropWhile(array, predicate)

Creates a new array with elements dropped from the beginning until the predicate returns falsy.

Parameters

  • array (Array): The source array to drop elements from.
  • predicate (Function): The function invoked per iteration.

Returns

  • (Array): A new array with elements dropped.

Example

const sourceArray = [1, 2, 3, 4, 5];
const droppedWhile = arrayUtils.dropWhile(sourceArray, num => num < 3);
// Result: [3, 4, 5]

arrayUtils.fill(array, value, start, end)

Fills elements of an array with the specified value.

Parameters

  • array (Array): The source array to fill.
  • value (*): The value to fill the array with.
  • start (number): Optional. The start position.
  • end (number): Optional. The end position.

Returns

  • (Array): The modified array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const filled = arrayUtils.fill(sourceArray, 'a', 1, 3);
// Result: [1, 'a', 'a', 4, 5]

arrayUtils.findIndex(array, predicate)

Gets the index of the first element in the array that satisfies the predicate.

Parameters

  • array (Array): The source array to search.
  • predicate (Function): The function invoked per iteration.

Returns

  • (number): The index of the found element, or -1 if not found.

Example

const sourceArray = [1, 2, 3, 4, 5];
const index = arrayUtils.findIndex(sourceArray, num => num === 3);
// Result: 2

arrayUtils.findLastIndex(array, predicate)

Gets the index of the last element in the array that satisfies the predicate.

Parameters

  • array (Array): The source array to search.
  • predicate (Function): The function invoked per iteration.

Returns

  • (number): The index of the found element, or -1 if not found.

Example

const sourceArray = [1, 2, 3, 2, 1];
const lastIndex = arrayUtils.findLastIndex(sourceArray, num => num === 2);
// Result: 3

arrayUtils.first(array)

Gets the first element of an array.

Parameters

  • array (Array): The source array.

Returns

  • (*): The first element of the array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const firstElement = arrayUtils.first(sourceArray);
// Result: 1

arrayUtils.flatten(array)

Flattens a nested array structure.

Parameters

  • array (Array): The nested array to flatten.

Returns

  • (Array): A new flattened array.

Example

const sourceArray = [1, [2, [3, [4]], 5]];
const flattened = arrayUtils.flatten(sourceArray);
// Result: [1, 2, [3, [4]], 5]

arrayUtils.flattenDeep(array)

Recursively flattens a nested array structure.

Parameters

  • array (Array): The nested array to deeply flatten.

Returns

  • (Array): A new deeply flattened array.

Example

const sourceArray = [1, [2, [3, [4]], 5]];
const deeplyFlattened = arrayUtils.flattenDeep(sourceArray);
// Result: [1, 2, 3, 4, 5]

arrayUtils.flattenDepth(array, depth)

Flattens a nested array structure up to a specified depth.

Parameters

  • array (Array): The nested array to flatten. depth (number): The maximum depth to flatten.

Returns

  • (Array): A new flattened array.

Example

const sourceArray = [1, [2, [3, [4]], 5]];
const flattenedToDepth = arrayUtils.flattenDepth(sourceArray, 1);
// Result: [1, 2, [3, [4]], 5]

arrayUtils.fromPairs(pairs)

Creates an object composed from key-value pairs.

Parameters

pairs (Array): The key-value pairs.

Returns

  • (Object): The new object.

Example

const sourcePairs = [['a', 1], ['b', 2], ['c', 3]];
const objFromPairs = arrayUtils.fromPairs(sourcePairs);
// Result: { a: 1, b: 2, c: 3 }

arrayUtils.head(array)

Gets the first element of an array.

Parameters

  • array (Array): The source array.

Returns

  • (*): The first element of the array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const firstElement = arrayUtils.head(sourceArray);
// Result: 1

arrayUtils.indexOf(array, value, fromIndex)

Gets the index at which the first occurrence of value is found.

Parameters

  • array (Array): The source array to search.
  • value (*): The value to search for.
  • fromIndex (number): Optional. The index to start searching from.

Returns

  • (number): The index of the found element, or -1 if not found.

Example

const sourceArray = [1, 2, 3, 4, 5];
const index = arrayUtils.indexOf(sourceArray, 3);
// Result: 2

arrayUtils.initial(array)

Gets all but the last element of an array.

Parameters

  • array (Array): The source array.

Returns

  • (Array): A new array excluding the last element.

Example

const sourceArray = [1, 2, 3, 4, 5];
const initial = arrayUtils.initial(sourceArray);
// Result: [1, 2, 3, 4]

arrayUtils.intersection(arrays)

Creates an array of unique values that are included in all source arrays.

Parameters

  • arrays (...Array): The arrays to inspect.

Returns

  • (Array): The new array of intersecting values.

Example

const array1 = [2, 1];
const array2 = [2, 3];
const intersected = arrayUtils.intersection(array1, array2);
// Result: [2]

arrayUtils.intersectionBy(arrays, iteratee)

Creates an array of unique values that are included in all source arrays, determined by an iteratee.

Parameters

  • arrays (...Array): The arrays to inspect.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (Array): The new array of intersecting values.

Example

const array1 = [{ 'x': 2 }, { 'x': 1 }];
const array2 = [{ 'x': 1 }];
const intersected = arrayUtils.intersectionBy(array1, array2, 'x');
// Result: [{ 'x': 1 }]

arrayUtils.intersectionWith(arrays, comparator)

Creates an array of unique values that are included in all source arrays, determined by a custom comparator.

Parameters

  • arrays (...Array): The arrays to inspect.
  • comparator (Function): The comparator function to determine values.

Returns

  • (Array): The new array of intersecting values.

Example

const array1 = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
const array2 = [{ 'x': 1, 'y': 2 }];
const intersected = arrayUtils.intersectionWith(array1, array2, (a, b) => a.x === b.x && a.y === b.y);
// Result: [{ 'x': 1, 'y': 2 }]

arrayUtils.join(array, separator)

Converts all elements in an array into a string, separated by the provided separator.

Parameters

  • array (Array): The source array to be joined. separator (string): The separator to use between elements.

Returns

  • (string): The joined string.

Example

const sourceArray = ['a', 'b', 'c'];
const joined = arrayUtils.join(sourceArray, '-');
// Result: 'a-b-c'

arrayUtils.last(array)

Gets the last element of an array.

Parameters

  • array (Array): The source array.

Returns

  • (*): The last element of the array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const lastElement = arrayUtils.last(sourceArray);
// Result: 5

arrayUtils.lastIndexOf(array, value, fromIndex)

Gets the index at which the last occurrence of value is found.

Parameters

  • array (Array): The source array to search.
  • value (*): The value to search for.
  • fromIndex (number): Optional. The index to start searching from.

Returns

  • (number): The index of the found element, or -1 if not found.

Example

const sourceArray = [1, 2, 3, 2, 1];
const lastIndex = arrayUtils.lastIndexOf(sourceArray, 2);
// Result: 3

arrayUtils.nth(array, n)

Gets the element at index n of an array.

Parameters

  • array (Array): The source array.
  • n (number): The index to retrieve the element from.

Returns

  • (*): The element at index n.

Example

const sourceArray = [1, 2, 3, 4, 5];
const nthElement = arrayUtils.nth(sourceArray, 2);

arrayUtils.pull(array, ...values)

Removes all given values from an array.

Parameters

  • array (Array): The source array.
  • values (...*): The values to remove.

Returns

  • (Array): The modified array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const modifiedArray = arrayUtils.pull(sourceArray, 2, 4);
// Result: [1, 3, 5]

arrayUtils.pullAll(array, values)

Removes all elements in values from an array.

Parameters

  • array (Array): The source array.
  • values (Array): The values to remove.

Returns

  • (Array): The modified array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const valuesToRemove = [2, 4];
const modifiedArray = arrayUtils.pullAll(sourceArray, valuesToRemove);
// Result: [1, 3, 5]

arrayUtils.pullAllBy(array, values, iteratee)

Removes all elements from an array that match the values determined by an iteratee.

Parameters

  • array (Array): The source array.
  • values (Array): The values to remove.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (Array): The modified array.

Example

const sourceArray = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }];
const valuesToRemove = [{ 'x': 1 }, { 'x': 3 }];
const modifiedArray = arrayUtils.pullAllBy(sourceArray, valuesToRemove, 'x');
// Result: [{ 'x': 2 }]

arrayUtils.pullAllWith(array, values, comparator)

Removes all elements from an array that match the values determined by a custom comparator.

Parameters

  • array (Array): The source array.
  • values (Array): The values to remove.
  • comparator (Function): The comparator function to determine values.

Returns

  • (Array): The modified array.

Example

const sourceArray = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 3, 'y': 4 }];
const valuesToRemove = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }];
const modifiedArray = arrayUtils.pullAllWith(sourceArray, valuesToRemove, (a, b) => a.x === b.x && a.y === b.y);
// Result: [{ 'x': 2, 'y': 1 }]

arrayUtils.pullAt(array, indexes)

Removes elements from an array at specified indexes.

Parameters

  • array (Array): The source array. indexes (number[]): The indexes to remove elements from.

Returns

  • (Array): The modified array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const indexesToRemove = [1, 3];
const modifiedArray = arrayUtils.pullAt(sourceArray, indexesToRemove);
// Result: [1, 3, 5]

arrayUtils.remove(array, predicate)

Removes all elements from an array that satisfy the provided predicate.

Parameters

  • array (Array): The source array.
  • predicate (Function): The function invoked per iteration.

Returns

  • (Array): The modified array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const modifiedArray = arrayUtils.remove(sourceArray, num => num % 2 === 0);
// Result: [1, 3, 5]

arrayUtils.reverse(array)

Reverses the elements of an array.

Parameters

  • array (Array): The source array.

Returns

  • (Array): The modified array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const reversedArray = arrayUtils.reverse(sourceArray);
// Result: [5, 4, 3, 2, 1]

arrayUtils.slice(array, start, end)

Creates a slice of an array from start up to, but not including, end.

Parameters

  • array (Array): The source array.
  • start (number): Optional. The start position.
  • end (number): Optional. The end position.

Returns

  • (Array): The sliced array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const slicedArray = arrayUtils.slice(sourceArray, 1, 4);
// Result: [2, 3, 4]

arrayUtils.sortedIndex(array, value)

Gets the index at which value should be inserted into a sorted array.

Parameters

  • array (Array): The sorted source array.
  • value (*): The value to evaluate.

Returns

  • (number): The index at which value should be inserted.

Example

const sourceArray = [1, 3, 5, 7];
const insertionIndex = arrayUtils.sortedIndex(sourceArray, 4);
// Result: 2

arrayUtils.sortedIndexBy(array, value, iteratee)

Gets the index at which value should be inserted into a sorted array, determined by an iteratee.

Parameters

  • array (Array): The sorted source array.
  • value (*): The value to evaluate.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (number): The index at which value should be inserted.

Example

const sourceArray = [{ 'x': 10 }, { 'x': 20 }, { 'x': 30 }];
const insertionIndex = arrayUtils.sortedIndexBy(sourceArray, { 'x': 25 }, 'x');
// Result: 2

arrayUtils.sortedIndexOf(array, value)

Gets the index at which the first occurrence of value is found in a sorted array.

Parameters

  • array (Array): The sorted source array.
  • value (*): The value to search for.

Returns

  • (number): The index of the found element, or -1 if not found.

Example

const sourceArray = [1, 2, 2, 3, 4];
const index = arrayUtils.sortedIndexOf(sourceArray, 2);
// Result: 1

arrayUtils.sortedLastIndex(array, value)

Gets the index at which value should be inserted into a sorted array to maintain order.

Parameters

  • array (Array): The sorted source array.
  • value (*): The value to evaluate.

Returns

  • (number): The index at which value should be inserted.

Example

const sourceArray = [1, 3, 5, 7];
const insertionIndex = arrayUtils.sortedLastIndex(sourceArray, 4);
// Result: 2

arrayUtils.sortedLastIndexBy(array, value, iteratee)

Gets the index at which value should be inserted into a sorted array to maintain order, determined by an iteratee.

Parameters

  • array (Array): The sorted source array.
  • value (*): The value to evaluate.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (number): The index at which value should be inserted.

Example

const sourceArray = [{ 'x': 10 }, { 'x': 20 }, { 'x': 30 }];
const insertionIndex = arrayUtils.sortedLastIndexBy(sourceArray, { 'x': 25 }, 'x');
// Result: 2

arrayUtils.sortedLastIndexOf(array, value)

Gets the index at which the last occurrence of value is found in a sorted array.

Parameters

  • array (Array): The sorted source array.
  • value (*): The value to search for.

Returns

  • (number): The index of the found element, or -1 if not found.

Example

const sourceArray = [1, 2, 2, 3, 4];
const lastIndex = arrayUtils.sortedLastIndexOf(sourceArray, 2);
// Result: 2

arrayUtils.sortedUniq(array)

Creates a duplicate-free version of a sorted array.

Parameters

  • array (Array): The sorted source array.

Returns

  • (Array): A new duplicate-free array.

Example

const sourceArray = [1, 2, 2, 3, 4, 4, 4, 5];
const uniqArray = arrayUtils.sortedUniq(sourceArray);
// Result: [1, 2, 3, 4, 5]

arrayUtils.sortedUniqBy(array, iteratee)

Creates a duplicate-free version of a sorted array, determined by an iteratee.

Parameters

  • array (Array): The sorted source array.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (Array): A new duplicate-free array.

Example

const sourceArray = [{ 'x': 1 }, { 'x': 1 }, { 'x': 2 }];
const uniqArray = arrayUtils.sortedUniqBy(sourceArray, 'x');
// Result: [{ 'x': 1 }, { 'x': 2 }]

arrayUtils.tail(array)

Gets all but the first element of an array.

Parameters

  • array (Array): The source array.

Returns

  • (Array): A new array excluding the first element.

Example

const sourceArray = [1, 2, 3, 4, 5];
const tailArray = arrayUtils.tail(sourceArray);
// Result: [2, 3, 4, 5]

arrayUtils.take(array, n)

Creates a slice of an array with n elements taken from the beginning.

Parameters

  • array (Array): The source array.
  • n (number): The number of elements to take.

Returns

  • (Array): A new array with elements taken.

Example

const sourceArray = [1, 2, 3, 4, 5];
const takenArray = arrayUtils.take(sourceArray, 3);
// Result: [1, 2, 3]

arrayUtils.takeRight(array, n)

Creates a slice of an array with n elements taken from the end.

Parameters

  • array (Array): The source array.
  • n (number): The number of elements to take.

Returns

  • (Array): A new array with elements taken.

Example

const sourceArray = [1, 2, 3, 4, 5];
const takenRightArray = arrayUtils.takeRight(sourceArray, 3);
// Result: [3, 4, 5]

arrayUtils.takeRightWhile(array, predicate)

Creates a slice of an array with elements taken from the end until the predicate returns falsy.

Parameters

  • array (Array): The source array.
  • predicate (Function): The function invoked per iteration.

Returns

  • (Array): A new array with elements taken.

Example

const sourceArray = [1, 2, 3, 4, 5];
const takenRightWhileArray = arrayUtils.takeRightWhile(sourceArray, num => num > 3);
// Result: [4, 5]

arrayUtils.takeWhile(array, predicate)

Creates a slice of an array with elements taken from the beginning until the predicate returns falsy.

Parameters

  • array (Array): The source array.
  • predicate (Function): The function invoked per iteration.

Returns

  • (Array): A new array with elements taken.

Example

const sourceArray = [1, 2, 3, 4, 5];
const takenWhileArray = arrayUtils.takeWhile(sourceArray, num => num < 3);
// Result: [1, 2]

arrayUtils.union(arrays)

Creates an array of unique values, in order, from all source arrays.

Parameters

  • arrays (...Array): The arrays to inspect.

Returns

  • (Array): The new array of unique values.

Example

const array1 = [2];
const array2 = [1, 2];
const unionArray = arrayUtils.union(array1, array2);
// Result: [2, 1]

arrayUtils.unionBy(arrays, iteratee)

Creates an array of unique values, in order, from all source arrays, determined by an iteratee.

Parameters

  • arrays (...Array): The arrays to inspect.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (Array): The new array of unique values.

Example

const array1 = [2.1];
const array2 = [1.2, 2.3];
const unionArray = arrayUtils.unionBy(array1, array2, Math.floor);
// Result: [2.1, 1.2]

arrayUtils.unionWith(arrays, comparator)

Creates an array of unique values, in order, from all source arrays, determined by a custom comparator.

Parameters

  • arrays (...Array): The arrays to inspect.
  • comparator (Function): The comparator function to determine values.

Returns

  • (Array): The new array of unique values.

Example

const array1 = [{ 'x': 1, 'y': 2 }];
const array2 = [{ 'x': 2, 'y': 1 }];
const unionArray = arrayUtils.unionWith(array1, array2, (a, b) => a.x === b.x && a.y === b.y);
// Result: [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]

arrayUtils.uniq(array)

Creates a duplicate-free version of an array.

Parameters

  • array (Array): The source array.

Returns

  • (Array): A new duplicate-free array.

Example

const sourceArray = [2, 1, 2, 3];
const uniqArray = arrayUtils.uniq(sourceArray);
// Result: [2, 1, 3]

arrayUtils.uniqBy(array, iteratee)

Creates a duplicate-free version of an array, determined by an iteratee.

Parameters

  • array (Array): The source array.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (Array): A new duplicate-free array.

Example

const sourceArray = [{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }];
const uniqArray = arrayUtils.uniqBy(sourceArray, 'x');
// Result: [{ 'x': 1 }, { 'x': 2 }]

arrayUtils.uniqWith(array, comparator)

Creates a duplicate-free version of an array, determined by a custom comparator.

Parameters

  • array (Array): The source array.
  • comparator (Function): The comparator function to determine values.

Returns

  • (Array): A new duplicate-free array.

Example

const sourceArray = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
const uniqArray = arrayUtils.uniqWith(sourceArray, (a, b) => a.x === b.x && a.y === b.y);
// Result: [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]

arrayUtils.unzip(array)

Creates an array of grouped elements, the first of which contains the first elements of the given arrays, and so on.

Parameters

  • array (Array): The source array.

Returns

  • (Array): The new array of grouped elements.

Example

const sourceArray = [['a', 1, true], ['b', 2, false]];
const unzippedArray = arrayUtils.unzip(sourceArray);
// Result: [['a', 'b'], [1, 2], [true, false]]

arrayUtils.unzipWith(array, iteratee)

This method is like arrayUtils.unzip except that it accepts an iteratee which is invoked for each group of elements.

Parameters

  • array (Array): The source array.
  • iteratee (Function): The function to combine elements.

Returns

  • (Array): The new array of combined elements.

Example

const sourceArray = [[1, 10, 100], [2, 20, 200]];
const combinedArray = arrayUtils.unzipWith(sourceArray, (...args) => arrayUtils.sum(args));
// Result: [3, 30, 300]

arrayUtils.without(array, ...values)

Creates an array excluding all given values.

Parameters

  • array (Array): The source array. values (...*): The values to exclude.

Returns

  • (Array): The new array.

Example

const sourceArray = [1, 2, 3, 4, 5];
const newArray = arrayUtils.without(sourceArray, 2, 4);
// Result: [1, 3, 5]

arrayUtils.xor(arrays)

Creates an array of unique values that is the symmetric difference of the given arrays.

Parameters

  • arrays (...Array): The arrays to inspect.

Returns

  • (Array): The new array of unique values.

Example

const array1 = [2, 1];
const array2 = [2, 3];
const xorArray = arrayUtils.xor(array1, array2);
// Result: [1, 3]

arrayUtils.xorBy(arrays, iteratee)

Creates an array of unique values that is the symmetric difference of the given arrays, determined by an iteratee.

Parameters

  • arrays (...Array): The arrays to inspect.
  • iteratee (Function|Object|string): The iteratee to determine values.

Returns

  • (Array): The new array of unique values.

Example

const array1 = [2.1, 1.2];
const array2 = [2.3, 3.4];
const xorArray = arrayUtils.xorBy(array1, array2, Math.floor);
// Result: [1.2, 3.4]

arrayUtils.xorWith(arrays, comparator)

Creates an array of unique values that is the symmetric difference of the given arrays, determined by a custom comparator.

Parameters

  • arrays (...Array): The arrays to inspect.
  • comparator (Function): The comparator function to determine values.

Returns

  • (Array): The new array of unique values.

Example

const array1 = [{ 'x': 1, 'y': 2 }];
const array2 = [{ 'x': 2, 'y': 1 }];
const xorArray = arrayUtils.xorWith(array1, array2, (a, b) => a.x === b.x && a.y === b.y);
// Result: [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]

arrayUtils.zip(...arrays)

Creates an array of grouped elements, the first of which contains the first elements of the given arrays, and so on.

Parameters

  • arrays (...Array): The arrays to process.

Returns

  • (Array): The new array of grouped elements.

Example

const array1 = [1, 2, 3];
const array2 = ['a', 'b', 'c'];
const zippedArray = arrayUtils.zip(array1, array2);
// Result: [[1, 'a'], [2, 'b'], [3, 'c']]

arrayUtils.zipObject(props, values)

Creates an object composed from arrays of property names and corresponding values.

Parameters

  • props (Array): The property names.
  • values (Array): The corresponding values.

Returns

  • (Object): The new object.

Example

const propertyNames = ['a', 'b', 'c'];
const values = [1, 2, 3];
const zippedObject = arrayUtils.zipObject(propertyNames, values);
// Result: { 'a': 1, 'b': 2, 'c': 3 }

arrayUtils.zipObjectDeep(props, values)

This method is like arrayUtils.zipObject except that it supports property paths.

Parameters

  • props (Array): The property names or paths.
  • values (Array): The corresponding values.

Returns

  • (Object): The new object.

Example

const propertyPaths = ['a.b[0].c', 'a.b[1].d'];
const values = [1, 2];
const zippedObject = arrayUtils.zipObjectDeep(propertyPaths, values);
// Result: { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }

arrayUtils.zipWith(arrays, iteratee)

This method is like arrayUtils.zip except that it accepts an iteratee to specify how grouped values should be combined.

Parameters

  • arrays (...Array): The arrays to process.
  • iteratee (Function): The function to combine grouped values.

Returns

  • (Array): The new array of combined elements.

Example

const array1 = [1, 2, 3];
const array2 = [10, 20, 30];
const combinedArray = arrayUtils.zipWith(array1, array2, (a, b) => a + b);
// Result: [11, 22, 33]