Utilities

Chunk

Creates a List of elements split into groups the length of size. If List can't be split evenly, the final chunk will be the remaining elements

Arguments

  1. list (List): The array to process.

  2. [size=1] (num): The length of each chunk

Returns

(List): Returns the new list of chunks.

chunk(['a', 'b', 'c', 'd'], 2);
// [['a', 'b'],['c', 'd']]

Compact

Creates an array with all falsey values removed. The values false, null, 0, "", are falsey.

Arguments

  1. list (List): The array to compact.

Returns

(List): Returns the new array of filtered values.

compact([0, 1, false, 2, '', 3]);
// [1, 2, 3]

Difference

Creates an array of array values not included in the other given list using SameValueZerofor equality comparisons. The order and references of result values are determined by the first list.

Arguments

  1. list (List): The array to inspect.

  2. [values] (...List): The values to exclude.

Returns

(List): Returns the new list of filtered values.

difference([2, 1], [2, 3]);
/// [1]

DifferenceBy

This method is like difference except that it accepts iteratee which is invoked for each element nth of the List and values to generate the criterion by which they're compared. The order and references of result values are determined by the first List.The iteratee is invoked with one argument.

Arguments

  1. list (List): The array to inspect.

  2. [values] (...List): The values to exclude.

  3. [iteratee] (Function): The iteratee invoked per element.

Returns

(List): Returns the new list of filtered values.

differenceBy([2.1, 1.2], [2.1, 3.4], (a) => a);
//[1.2]

Drop

Creates a slice of List with n elements dropped from the beginning

Arguments

  1. list (List): The list to query.

  2. n=1 (num): The number of elements to drop.

Returns

(List): Returns the slice of list.

drop([1, 2, 3], 2)
// [3]

DropRight

Creates a slice of List with n elements dropped from the end.

Arguments

  1. list (List): The list to query.

  2. n=1 (num): The number of elements to drop.

Returns

(List): Returns the slice of list.

dropRight([1, 2, 3], 2);
// [1]

DropWhile

Creates a slice of list excluding elements dropped from the beginning.Elements are dropped until predicate returns falsey. The predicate is invoked with three arguments: (value, index, list).

Arguments

  1. list (List): The array to query.

  2. [predicate] (Function): The function invoked per iteration.

Returns

(List): Returns the slice of list.

dropWhile([1, 2, 3], (a) => a < 2)
// [1, 2, 3]

Fill

Fills elements of a list with value from start up to, but not including, end.

Arguments

  1. list (List): The list to fill.

  2. value (*): The value to fill list with.

  3. [start=0] (num): The start position.

  4. [end=list.length] (num): The end position.

Returns

(List): Returns list

fill([1, 2, 3], 2, start: 0)
// [2,2,2]

Last updated