Arrays

Arrays ⇐ Class

Kind: global class
Extends: Class

Arrays()

Array utility methods as a static class.

search(arr, value, [propertyName]) ⇒ number

Search a value in an unordered list, or a property value in an unordered object list. A lot faster than Array.prototype.findIndex Algorithmic complexity : O(n) If the array is sorted, use binarySearch instead.

Kind: static method of Arrays
Returns: number - The index in the array of the found element. Otherwise -1.

ParamTypeDescription
arrArray

the array.

value*

the value to look for, tested by strict equality ===

[propertyName]string

if given, element[propertyName] is compared. If not, element is compared.

shuffle(arr) ⇒ Array

Shuffles an array with the Fisher-Yates algorithm. The array is shuffled in-place and not copied.

Kind: static method of Arrays

ParamTypeDescription
arrArray

the array to shuffle.

binarySearch(arr, value, [propertyName], [options]) ⇒ number

Fast search of a value in an array of values, or a property value in an array of objects. The objects in the array must be sorted in ascending order of this property. https://en.wikipedia.org/wiki/Binary_search_algorithm This algorithm is a lot faster than naive search, but needs the array to be sorted in the first place. Algorithmic complexity : O(log n)

Kind: static method of Arrays
Returns: number - - An index (positive or negative). See options for more details.

ParamTypeDefaultDescription
arrArray

The array to search.

value*

the value to look for, tested by strict equality ===

[propertyName]string

if given, element[propertyName] is compared. If not, element is compared.

[options]Object
[options.strategy]string"match"

If given, define the strategy to adopt to the final index. If options.strategy equals 'match', it returns a positive index in the array of the found element. Otherwise -1-insertionPoint where insertionPoint is the index where the element would be inserted. If options.strategy equals 'floor', it returns the greater index smaller than value in the array. If the value is smaller than the first element value, it returns -1. If options.strategy equals 'round', it returns the nearest index of the value in the array. If options.strategy equals 'ceil', it returns the smallest index greater than value in the array. If the value is greater than the last element value, it returns array.length.

[options.forceArrayRange]booleanfalse

If true, it makes sure the returned index is in array range. Used when strategy is 'floor' or 'ceil'. If the returned index was -1 it will return 0 and if it was arr.length, it will return arr.length-1.

interpolationSearch(arr, value, [propertyName], [options]) ⇒ number

Fast search of a value in an array of values, or a property value in an array of objects. The objects in the array must be sorted in ascending order of this property. https://en.wikipedia.org/wiki/Interpolation_search This algorithm is a lot faster than naïve search, but needs the array to be sorted in the first place. It is best used if the values in the array are uniformly distributed. Average algorithmic complexity : O(log log n) Worst algorithmic complexity (exponential distribution of values) : O(n)

Kind: static method of Arrays
Returns: number - - An index (positive or negative). See options for more details.

ParamTypeDefaultDescription
arrArray

The array to search.

value*

the value to look for, tested by strict equality ===

[propertyName]string

if given, element[propertyName] is compared. If not, element is compared.

[options]Object
[options.strategy]Objectmatch

If given, define the strategy to adopt to the final index. If options.strategy equals 'match', it returns a positive index in the array of the found element. Otherwise -1-insertionPoint where insertionPoint is the index where the element would be inserted. If options.strategy equals 'floor', it returns the greater index smaller than value in the array. If the value is smaller than the first element value, it returns -1. If options.strategy equals 'round', it returns the nearest index of the value in the array. If options.strategy equals 'ceil', it returns the smallest index greater than value in the array. If the value is greater than the last element value, it returns array.length.

[options.forceArrayRange]Objectfalse

If true, it makes sure the returned index is in array range. Used when strategy is 'floor' or 'ceil'. If the returned index was -1 it will return 0 and if it was arr.length, it will return arr.length-1.

move(arr, old_index, new_index) ⇒ Array

Move an element in array

Kind: static method of Arrays

ParamTypeDescription
arrArray
old_indexnumber

(integer)

new_indexnumber

(integer)

Example

Arrays.move([1, 2, 3], 0, 1) gives [2, 1, 3].

toSet(arr, [propertyName]) ⇒ Object.<(number|string), boolean>

Transforms an array of numbers or strings into a HashSet.

Kind: static method of Arrays

ParamTypeDescription
arrArray

the source array. Can be undefined.

[propertyName]string

optional property name if the elements of the array are objects.

Example

$Arrays.toSet([2, 4, 7]) => { 2: true, 4: true, 7: true }
$Arrays.toSet([{id:2}, {id:4}, {id:7}], 'id') => { 2: true, 4: true, 7: true }

rotate(arr, delta)

Rotate an array in-place by “delta” positions.

Kind: static method of Arrays

ParamTypeDescription
arrArray

the array to rotate

deltanumber

number of positions to rotate (to the left if positive, to the right if negative)

Example

$Arrays.rotate([1,2,3,4,5], 1) === [2, 3, 4, 5, 1]
$Arrays.rotate([1,2,3,4,5], -1) === [5, 1, 2, 3, 4]

Reversal algorithm from Programming Pearls.
Algorithmic complexity: O(n)
Space complexity: O(1)

Approx 5 times faster than push(shift) and unshift(pop).
Optimizing for delta === 1 or 2 isn't faster.

equals(arr1, arr2) ⇒ boolean

Compare 2 arrays - loop through them and compare every value. Non-primitive elements (objects, sub-arrays) are compared by reference.

Kind: static method of Arrays

ParamType
arr1Array
arr2Array

Example

$Arrays.equals([1, 2, [3, 4]], [1, 2, [3, 4]]) === false;
$Arrays.equals([1, 2, 3, 7], [1, 2, 3, 7]) === true;
$Arrays.equals([1, 2, 7, 3], [1, 2, 3, 7]) === false;