Arrays

Functions utils for arrays

Methods

(static) allEqual(arr) → {boolean}

Check if all Elements of the array are equal

Parameters:
NameTypeDescription
arrany

the array to check all elements

Returns:

true if all elements of the array ara equal

Type: 
boolean
Example
allEqual([0,0,0,0]) // true
allEqual([0,0,0,1]) // false
allEqual([0,"a","a"]) // false
allEqual([[1,1],[1,1]]) // true
allEqual([[1,1],[1,0]]) // false
allEqual([{a:"b",c:1},{a:"b"}]) // false
allEqual([{a:"b",c:1},{a:"b",c:1}]) // true
allEqual([{a:"b",c:1},{a:"b",c:1},{a:"c",c:1}]) // false
allEqual([{a:"b",c:1},{a:"b",c:1},{a:"b",c:1}]) // true

(static) choice(array) → {any}

Returns a random item from the array

Parameters:
NameTypeDescription
arrayArray.<any>

the array to select a random item

Returns:
Type: 
any
Examples
choice(["A", "B", "C"]); // "B"
choice([10, 5, 123]); // 10

(static) chunk(array, size)

Splits an array into smaller arrays of a specified size.

Parameters:
NameTypeDescription
arrayArray

the array to split into smaller arrays

sizenumber

the number of elements to split

Returns:

an array of smaller arrays

Example
chunk(["A", "B", "C", "D", "E", "F", "G"], 2); // [["A", "B"], ["C", "D"], ["E", "F"], ["G"]]

(static) findBigObject(array, prop, returnOnlyValueopt) → {any|number}

Returns the biggest element of a array of objects.

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<any>

The array to search

propstring

The property to find the biggest element

returnOnlyValueboolean<optional>
false

If true only returns the value of the given property with the biggest value

Returns:
  • The biggest element in the array
Type: 
any | number
Example
const myArray = [{a:1, b:100}, {a: 0, b:50}, {a:0, b:200}]
findBigObject(myArray, "b"); // {a:0, b:200}
findBigObject(myArray, "a"); // {a:1, b:100}
findBigObject(myArray, "b", true); // 200
findBigObject(myArray, "a", true); // 1

(static) findBigObjectDeprecated(array, prop) → {Object}

Parameters:
NameTypeDescription
arrayArray.<Object>

The array to search

propstring

The property to find the biggest element

Deprecated
  • Yes
Returns:
  • The biggest element in the array
Type: 
Object

(static) findLowObject(array, prop, returnOnlyValueopt) → {Object}

Returns the lowest element from array of objects

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<Object>

The array to search

propstring

The property to find the lowest element

returnOnlyValueboolean<optional>
false

If true only returns the value of the given property with the lowest value

Returns:
  • The lowest element in the array
Type: 
Object
Example
const myArray = [{a:1, b:100}, {a:10, b:50}, {a:0, b:200}]
findLowObject(myArray, "b"); // {a:10, b:50}
findLowObject(myArray, "a"); // {a:0, b:200}
findLowObject(myArray, "b", true); // 50
findLowObject(myArray, "a", true); // 0

(static) findLowObjectDeprecated(array, prop) → {Object}

Parameters:
NameTypeDescription
arrayArray.<Object>

The array to search

propstring

The property to find the lowest element

Deprecated
  • Yes
Returns:
  • The lowest element in the array
Type: 
Object

(static) isItemInCommon(arr1, arr2) → {boolean}

Checks if an item is in common between two arrays

Parameters:
NameTypeDescription
arr1Array.<any>

The first array

arr2Array.<any>

The second array

Returns:

true if the item is in common

Type: 
boolean
Example
isItemInCommon([1, 2, 3], [2, 3, 4]); // true
isItemInCommon(["A", "B", "C"], ["B", "C", "D"]) // true
isItemInCommon([1, 2, 3], [4, 5, 6]); // false

(static) isSorted(arr) → {boolean}

Check if the given array is sorted fom lowest to highest

Parameters:
NameTypeDescription
arrany

the array to check

Returns:

true if the array is sorted

Type: 
boolean
Example
isSorted([]) // true
isSorted([0,0,0,0]) // true
isSorted([2,1,4]) // false
isSorted([3,2,1]) // false
isSorted([1,2,3]) // true
isSorted(["B","A","D"]) // false
isSorted(["A","B","C"]) // true

(static) moveLeft(array, times) → {Array.<any>}

Move an array element to the right

Parameters:
NameTypeDescription
arrayArray.<any>

The array to move

timesnumber

The number of times to move the array

Returns:
Type: 
Array.<any>
Examples
moveLeft([1,2,3,4,5]); // [2,3,4,5,1]
moveLeft([1,2,3,4,5], 2); // [3,4,5,1,2]
moveLeft(["a","b","c","d","e"], 7) // ["c","d","e","a","b"]
moveLeft(["a","b"], 3) // ["b","a"]

(static) removeDuplicatesObj(array, property) → {Array}

Removes duplicates from an array of objects based on a specified property.

Parameters:
NameTypeDescription
arrayArray

The array of objects.

propertystring

The property to check for duplicates.

Returns:
  • A new array with duplicate objects removed.
Type: 
Array
Example
const array = [
{id:1,name:"John"},
{id:2,name:"Jane"},
{id:3,name:"John"},
{id:4,name:"Mike"},
{id:5,name:"Jane"}
];
removeDuplicates(array, "name"); // [{id:1,name:"John"}, {id:2,name:"Jane"}, {id:4,name:"Mike"}]

(static) shuffle(array, mutateOriginal) → {Array.<any>}

This function will randomize the position of the array items. (change the original array!)

To not mutate the original array pass in the mutateOriginal argument false, and this function will return a new array with the original items in random positions. (not changing the original array)

Parameters:
NameTypeDescription
arrayArray.<any>

the array with the items to randomize

mutateOriginalboolean

the array with the items to randomize

Returns:
Type: 
Array.<any>
Examples
shuffle(["A", "B", "C"]); // ["B","A","C"]
shuffle([1,2,3,4,5,6,7,8,9]); // [8,5,1,4,3,6,9,2,7]
shuffle([{a:1},{b:2},{c:3}]); // [{a:1},{c:3},{b:2}]

(static) sortAscending(arr, mutateOriginalopt) → {Array.<number>}

Sort an array of number by ascending

Parameters:
NameTypeAttributesDefaultDescription
arrArray.<number>

the array to sort

mutateOriginalboolean<optional>
false

if true will change the original array

Returns:

A new Array sorted

Type: 
Array.<number>
Example
const myArr = [10,4,2,7,1,0,11,4,2,3,5,8,4,3,0,6];
const myNewSortedArr = sortAscending(myArr); // [0,0,1,2,2,3,3,4,4,4,5,6,7,8,10,11]

(static) sortAscendingObject(arr, prop) → {Array.<any>}

Sort a array of objects in ascending order by property

Parameters:
NameTypeDescription
arrArray.<any>

the array to sort

propstring

the property base to sort

Returns:
  • a new sorted array by the given property
Type: 
Array.<any>

(static) sortDescending(arr) → {Array.<number>}

Sort an array of number by descending

Parameters:
NameTypeDescription
arrArray.<number>

the array to sort

Returns:

A new Array sorted

Type: 
Array.<number>
Example
const myArr = [10,4,2,7,1,0,11,4,2,3,5,8,4,3,0,6];
const myNewSortedArr = sortDescending(myArr); // [11,10,8,7,6,5,4,4,4,3,3,2,2,1,0,0]

(static) sortDescendingObject(arr, prop) → {Array.<any>}

Sort a array of objects in descending order by property

Parameters:
NameTypeDescription
arrArray.<any>

the array to sort

propstring

the property base to sort

Returns:
  • a new sorted array by the given property
Type: 
Array.<any>