Array

Methods

(static) array_compact(array) → {Array}

Source:
Since:
  • 0.1.0

Returns a copy of the array with all falsy values removed. In JavaScript, false, null, 0, "", undefined and NaN are all falsy.

Example
array_compact([0, 1, "Hello", {}, null, [1, 2, 3], void 0])
// => [1, "Hello", {}, [1, 2, 3]]
Parameters:
Name Type Description
array Array

Array to process.

Returns:

Returns array containing thruthy values.

Type
Array

(static) array_first(n, arropt) → {mix|Array}

Source:

Return the first element from the list. By providing number (n) as a first argument, result will be an array of the first n elements from a list.

Examples
array_first([1, 2, 3]);
// => 1
array_first([1, 2, 3], true);
// => [1]
array_first(2, [1, 2, 3, 4]);
// => [1, 2]
array_first(2)([1, 2, 3, 4]);
// => [1, 2]
Parameters:
Name Type Attributes Description
n Number | Array

Number of first values to get, or array.

arr Array | Boolean <optional>

The array to process, or array convert flag.

Returns:

Returns first value from array or the new array containing n values.

Type
mix | Array

(static) array_flatten(arr, shallowopt) → {Array}

Source:
Since:
  • 0.1.0

Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level.

Examples
array_flatten([1, [2], [3, [[4]]]])
// => [1, 2, 3, 4]
array_flatten([1, [2], [3, [[4]]]], true);
// => [1, 2, 3, [[4]]]
Parameters:
Name Type Attributes Description
arr Array

Array to process.

shallow Boolean <optional>

Flat only one level in depth.

Returns:

Returns flatten array.

Type
Array

(static) array_initial(n, arrayopt) → {Array}

Source:

Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the last n elements from the result.

Examples
array_initial([5, 4, 3, 2, 1]);
// => [5, 4, 3, 2]
array_initial(2, [5, 4, 3, 2, 1]);
// => [5, 4, 3]
array_initial(3)([5, 4, 3, 2, 1]);
// => [5, 4]
Parameters:
Name Type Attributes Description
n Number | Array

Number of last values to exclude, or array to process.

array Array <optional>

The array to process.

Returns:

Returns the new array containing initial values.

Type
Array

(static) array_intersection(array) → {Array}

Source:
Since:
  • 0.1.0

Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.

TODO: Performance optimization.

Example
array_intersection([1, 2, 3], [101, 2, 1, 10], [2, 1])
// => [1, 2]
Parameters:
Name Type Description
array Array

Array to process.

Returns:

Returns the new array of shared values.

Type
Array

(static) array_last(n, a) → {mix|Array}

Source:

Returns the last element of an array. Passing n will return the last n elements of the array.

Examples
array_last([5, 4, 3, 2, 1]);
// => 1
array_last([5, 4, 3, 2, 1], true);
// => [1]
array_last(2, [5, 4, 3, 2, 1]);
// => [2, 1]
array_last(2)([5, 4, 3, 2, 1]);
// => [2, 1]
Parameters:
Name Type Description
n Number | Array

Number of first elements to exclude or Array to process.

a Array | Boolean

Array to process or array convert flag.

Returns:

Returns last value or the new array containing last value(s)

Type
mix | Array

(static) array_pluck(key, array) → {Array}

Source:
Since:
  • 0.1.0

Retrieves the values of a specified property from all objects in the collection.

Example
array_pluck('id', [{id: 1}, {id:2}, {id:3}]);
// => [1, 2, 3]
Parameters:
Name Type Description
key String

Object key or nested object key path.

array Array

Array of objects to process.

Returns:

Returns the array with plucked values from object.

Type
Array

(static) array_rest(Number, arropt) → {Array}

Source:
Since:
  • 0.1.0

Returns the rest of the elements in an array. Pass an index to return the values of the array from that index onward.

Example
array_rest([1, 2, 3, 4]);
// => [2, 3, 4]
array_rest(2, [1, 2, 3, 4]);
// => [3, 4]
array_rest(2)([1, 2, 3, 4]);
// => [3, 4]
Parameters:
Name Type Attributes Description
Number Number | Array

of elements to skip or Array to process.

arr Array <optional>

Array to process

Returns:

Returns the new array containing rest values

Type
Array

(static) array_reverse(array) → {Array}

Source:
Since:
  • 0.1.0

Reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

Example
array_reverse([1, 2, 3, 4]);
// => [4, 3, 2, 1]
Parameters:
Name Type Description
array Array

Array to process.

Returns:

Returns reversed array.

Type
Array

(static) array_shuffle(array) → {Array}

Source:
Since:
  • 0.1.0

Returns a shuffled copy of the list, using a Fisher-Yates shuffle algorithm.

Built using Fisher–Yates shuffle algorithm. https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Example
array_shuffle([1, 2, 3, 4, 5 ,6]);
// => [2, 3, 1, 4, 6, 5]
Parameters:
Name Type Description
array Array

Array to process.

Returns:

Returns the new shuffled array.

Type
Array

(static) array_union(array) → {Array}

Source:
Since:
  • 0.1.0

Computes the union of the passed-in arrays or values: the list of unique items, in order, that are present in one or more of the arrays.

Examples
array_union([1, 2, 3], [101, 2, 1, 10], [2, 1])
// => [1, 2, 3, 101, 10]
array_union(1, 2, [3, 2], 4, [2, 5])
// => [1, 2, 3, 4, 5]
Parameters:
Name Type Description
array Array

Array to process

Returns:

Returns the new array of combined values.

Type
Array

(static) array_unique(array) → {Array}

Source:

Produces a duplicate-free version of the array.

TODO: Remove duplicate objects and inner arrays.

TODO: Performance optimization.

Example
array_unique([1, 2, 2, 3, 4, 3, 1, 5, 5, 7, 6, 6])
// => [1, 2, 3, 4, 5, 7, 6]
Parameters:
Name Type Description
array Array

Array to process.

Returns:

Returns array with duplicate free values.

Type
Array

(static) array_without(array) → {Array}

Source:
Since:
  • 0.1.0

Returns a copy of the array with all instances of the values removed.

TODO: Performance optimization.

Example
array_without([1, 2, 3, 4, 5], 2, 4)
// => [1, 3, 5]
Parameters:
Name Type Attributes Description
array Array

Array to process.

arguments <repeatable>
Returns:

Returns new array without v

Type
Array