ArrayUtils

ArrayUtils

Various utility methods that can be used on arrays

Constructor

new ArrayUtils()

Example
const arrayMethods = Collections.ArrayUtils;

Methods

(static) chunk(array, bitsopt) → {Array}

Splits the given array into chunks
Example
const myArray = [1, 2, 3, 4];
const altered = arrayMethods.chunk(myArray, 2);
// altered is [[1, 2], [3, 4]] ; myArray is unchanged
Parameters:
Name Type Attributes Default Description
array Array The array to chunk
bits number <optional>
0 The size of each nested array
Throws:
If @param bits is not a primitive number
Type
TypeError
Returns:
A new array split into @param bits
Type
Array

(static) filterNot(array, callback) → {Array}

Returns a new array with elements that give non-truthy values for the given testing function
Parameters:
Name Type Description
array Array The array to filter
callback function The function used to evaluate each element
Returns:
A new Array filled wit only elements that did not pass the test
Type
Array

(static) find(array, callback) → {number|undefined}

Finds the first occurrence in the given array where a given callback evaluates to truthy
Parameters:
Name Type Description
array Array The array to search through
callback function The function used to evaluate each element
Returns:
The element @callback is truthy or undefined if none passed
Type
number | undefined

(static) findIndex(array, callback) → {number}

Finds the index in the given array that passes the given testing function
Parameters:
Name Type Description
array Array The array to search through
callback function The function used to evaluate each element
Returns:
The index where @callback is truthy or -1 if none passed
Type
number

(static) flatten(array) → {Array}

Turns an n dimensional array into a 1 dimensional array
Example
const myArray = [[2], [3], [4, 5]];
const altered = arrayMethods.flatten(myArray);
// altered will be [2, 3, 4, 5] ; myArray is unchanged
Parameters:
Name Type Description
array Array The array to flatten
Returns:
@param array to a one dimensional array
Type
Array

(static) getRand(array) → {*}

Returns a random index in the given array
Example
const myArray = [1, 2];
const altered = arrayMethods.getRand(myArray);
// altered could be 1 or 2
Parameters:
Name Type Description
array Array The array to get random index from
Returns:
Random element in @param array
Type
*

(static) mapIf(array, test, mapper) → {Array}

Returns a new Array with elements mapped only if they pass a testing function first
Parameters:
Name Type Description
array Array The array to map to another array
test function The testing function
mapper function The mappping function
Returns:
A new Array with mapped elements that pass the @param test
Type
Array

(static) popMany(array, timesopt) → {Array}

Removes the last element from the given array
Example
const myArray = [1, 2, 3, 4];
const altered = arrayMethods.popMany(myArray, 3);
// myArray is [1, 2, 3, 4] ; altered is [1]
Parameters:
Name Type Attributes Default Description
array Array The array to pop
times number <optional>
0 The number of times to pop @param array
Returns:
A new array equal to [@param array - popped elements]
Type
Array

(static) pushMany(arrayopt, …args) → {Array}

Adds elements to the end of the given array
Example
const myArray = [1, 2];
const altered = arrayMethods.pushMany(myArray, "push", "me");
// myArray is unchanged ; altered = [1, 2, "push", "me"]
Parameters:
Name Type Attributes Default Description
array Array <optional>
empty array The array to push elements into
args * <repeatable>
Consecutive arguments to push into array
Returns:
A new array equal to [@param array + pushed elements]
Type
Array

(static) remove(array, indexopt) → {Array}

Removes the element at the given position in the given array
Example
const myArray = [1, 2, 3, 4];
let removedItems = arrayMethods.remove(myArray, 1);
// removedItems contains [2] and myArray is [1, 3, 4]
Parameters:
Name Type Attributes Default Description
array Array The array to remove elements from
index number <optional>
0 The index to remove from @param array
Returns:
Array of removed elements
Type
Array

(static) removeElement(array, predicate) → {Array}

Removes the first occurence of the given value from the array
Example
const myArray = [1, 2, 3, 4];
let removedItems = arrayMethods.removeElement(myArray, 3);
// changedArray contains [3] and myArray is [1, 2, 4]
Parameters:
Name Type Description
array Array The array to remove elements from
predicate function The function used to compare values
Returns:
Array of removed elements
Type
Array

(static) removeRand(array) → {Array}

Removes a random element from the given array
Example
const myArray = [1, 2];
const altered = arrayMethods.removeRand(myArray);
// altered could be 1 or 2 ; myArray's length decreases by 1
Parameters:
Name Type Description
array Array The array to remove a random element from
Returns:
An array of length 1 containing the element removed from @param array
Type
Array

(static) rotate(array, timesopt) → {Array}

Rotates the given array left(negative number) or right(positive number)
Example
const myArray = [1, 2, 3, 4];
let B = arrayMethods.rotate(myArray, 2);
// myArray is [1, 2, 3, 4]
// B is [3, 4, 1, 2]
B = arrayMethods.rotate(B, -2);
// B is back to original positioning of myArray [1, 2, 3, 4]
Parameters:
Name Type Attributes Default Description
array Array The array to rotate
times number <optional>
0 The number of times to rotate @param array
Throws:
If @param times is not a primitive number
Type
TypeError
Returns:
A new Array with rotations applied
Type
Array

(static) shiftMany(array, timesopt) → {Array}

Removes the first element from the given array
Example
const myArray = [1, 2, 3, 4];
const altered = arrayMethods.shiftMany(myArray, 3);
// myArray is [1, 2, 3, 4] ; altered is [4]
Parameters:
Name Type Attributes Default Description
array Array The array to shift
times number <optional>
0 The number of times to shift @param array
Returns:
A new array equal to [@param array - shifted elements]
Type
Array

(static) shuffle(array) → {undefined}

Shuffles the given array
Parameters:
Name Type Description
array Array The array to shuffle
Returns:
Type
undefined

(static) unshiftMany(arrayopt, …args) → {Array}

Adds elements to the front of the given array
Example
const myArray = [1, 2, 3, 4];
const altered = arrayMethods.unshiftMany(myArray, "hi");
// myArray is [1, 2, 3, 4] ; altered is ["hi", 1, 2, 3, 4]
Parameters:
Name Type Attributes Default Description
array Array <optional>
empty array The array to add elements into
args * <repeatable>
Consecutive arguments to push into array
Returns:
A new array equal to [unshifted elements + @param array ]
Type
Array