Class: ArrayUtils

ArrayUtils

new ArrayUtils()

Util methods about Array.
Source:

Methods

(static) changeIndex(targetArray, oldIndex, newIndex) → {Variant}

Change item at oldIndex to a new position.
Parameters:
Name Type Description
targetArray Array
oldIndex Int
newIndex Int
Source:
Returns:
item moved or null when oldIndex not in array.
Type
Variant

(static) changeItemIndex(targetArray, item, newIndex) → {Variant}

Change item in array to a new position.
Parameters:
Name Type Description
targetArray Array
item Variant
newIndex Int
Source:
Returns:
item or null when item not in array.
Type
Variant

(static) clone(src) → {Array}

Returns a new array with the same items as src.
Parameters:
Name Type Description
src Array
Source:
Returns:
Type
Array

(static) compare(a1, a2, itemCompareFunc)

Compare two arrays, from first to last items. If two items in each array is different, the one with the smaller item will be regarded as smaller array.
Parameters:
Name Type Description
a1 Array
a2 Array
itemCompareFunc function
Source:

(static) compareNestedArray(a1, a2, compareFunc) → {Int}

Compare two arrays. The array can be nested one and the nested children will also be compared. For instance: [3,2,1] > [2,3,1] [[2,3], 1] > [[1,2,3]] [[1,2],3,1] > [[1,2],3]
Parameters:
Name Type Description
a1 Array
a2 Array
compareFunc Func
Source:
Returns:
Type
Int

(static) divide(src, memberCount) → {Array}

Divide array into several small ones, each containing memberCount numbers of elements.
Parameters:
Name Type Description
src Array
memberCount Int
Source:
Returns:
Array of array.
Type
Array

(static) exclude(src, excludes) → {Array}

Subtract excluded items from source array.
Parameters:
Name Type Description
src Array
excludes Array
Source:
Returns:
Type
Array

(static) getMedian(arr) → {Number}

Returns median number of a numberic array.
Parameters:
Name Type Description
arr Array
Source:
Returns:
Type
Number

(static) getUniqueElemCount(a) → {Int}

Count elements in array. For example, [1, 2, 3, 3] will got return value 3
Parameters:
Name Type Description
a Array
Source:
Returns:
Type
Int

(static) group(arr, compareFunc) → {Array}

Compare all items in array and sort them into a new array. If compare result is 0 (equal), those items will be "grouped up" in a nested array. For example, var a = [1, 0, 1, 2, 3], the result of this method on a will be [0, [1, 1], 2, 3].
Parameters:
Name Type Description
arr Array
compareFunc Func
Source:
Returns:
Type
Array

(static) insertUnique(targetArray, obj) → {Int}

Insert obj to index of array and returns the index of newly inserted obj. If obj already inside array, position of obj will be changed.
Parameters:
Name Type Description
targetArray Array Target array.
obj Variant Must not be null.
Source:
Returns:
Index of obj in array.
Type
Int

(static) insertUniqueEx(targetArray, obj, index) → {Hash}

Insert obj to index of array and returns the a hash of {index(Int), isInserted(Bool)}. If obj already inside array, position of obj will be changed.
Parameters:
Name Type Description
targetArray Array Target array.
obj Variant Must not be null.
index Int Index to insert. If not set or less than 0, obj will be pushed to tail of array.
Source:
Returns:
{index, isPushed} hash. Index of obj in array.
Type
Hash

(static) intersect(a1, a2) → {Array}

Returns intersection of two arrays.
Parameters:
Name Type Description
a1 Array
a2 Array
Source:
Returns:
Type
Array

(static) isArray(value) → {Bool}

Check if value is an array.
Parameters:
Name Type Description
value Variant
Source:
Returns:
Type
Bool

(static) pushUnique(targetArray, obj) → {Int}

Append obj (or an array of obj) to the tail of array and returns the index of newly pushed obj. If obj already inside array, also returns index of obj in array.
Parameters:
Name Type Description
targetArray Array Target array.
obj Variant Must not be null.
Source:
Returns:
Index of obj in array.
Type
Int

(static) pushUniqueEx(targetArray, obj) → {Hash}

Append obj (or an array of obj) to the tail of array and returns the a hash of {index(Int), isPushed(Bool)}. If obj already inside array, returns index of obj and false.
Parameters:
Name Type Description
targetArray Array Target array.
obj Variant Must not be null.
Source:
Returns:
{index, isPushed} hash. Index of obj in array.
Type
Hash

(static) randomize(src)

Returns a new array that change the order of items in src array.
Parameters:
Name Type Description
src Array
Source:

(static) remove(targetArray, obj, removeAll) → {Object}

Remove an obj from targetArray. If success, returns obj. If obj not in array, returns null.
Parameters:
Name Type Description
targetArray Array
obj Object
removeAll Bool Whether all appearance of obj in array should be removed.
Source:
Returns:
Object removed or null.
Type
Object

(static) removeAt(targetArray, index) → {Object}

Remove item at index from targetArray. If success, returns removed item. If index not in array, returns null.
Parameters:
Name Type Description
targetArray Array
index Int
Source:
Returns:
Object removed or null.
Type
Object

(static) replace(targetArray, oldObj, newObj, replaceAll) → {Variant}

Replace oldObj in array with newObj.
Parameters:
Name Type Description
targetArray Array
oldObj Variant
newObj Variant
replaceAll Bool
Source:
Returns:
Object replaced or null.
Type
Variant

(static) reverse(a) → {Array}

Returns a reversed order array. For example, [1,2,3] will be turned to [3,2,1] after reversing.
Parameters:
Name Type Description
a Array
Source:
Returns:
Type
Array

(static) toArray()

If value is array, returns value directly, otherwise returns a array containing value.
Source:

(static) toUnique(a) → {Array}

Remove duplicated elements in array. For example, [1, 2, 3, 3] will got return value [1, 2, 3]
Parameters:
Name Type Description
a Array
Source:
Returns:
Type
Array