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 targetArrayArray oldIndexInt newIndexInt - 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 targetArrayArray itemVariant newIndexInt - 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 srcArray - 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 a1Array a2Array itemCompareFuncfunction - 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 a1Array a2Array compareFuncFunc - 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 srcArray memberCountInt - Source:
Returns:
Array of array.- Type
- Array
-
(static) exclude(src, excludes) → {Array}
-
Subtract excluded items from source array.
Parameters:
Name Type Description srcArray excludesArray - Source:
Returns:
- Type
- Array
-
(static) getMedian(arr) → {Number}
-
Returns median number of a numberic array.
Parameters:
Name Type Description arrArray - 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 aArray - 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 arrArray compareFuncFunc - 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 targetArrayArray Target array. objVariant 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 targetArrayArray Target array. objVariant Must not be null. indexInt 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 a1Array a2Array - Source:
Returns:
- Type
- Array
-
(static) isArray(value) → {Bool}
-
Check if value is an array.
Parameters:
Name Type Description valueVariant - 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 targetArrayArray Target array. objVariant 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 targetArrayArray Target array. objVariant 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 srcArray - 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 targetArrayArray objObject removeAllBool 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 targetArrayArray indexInt - 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 targetArrayArray oldObjVariant newObjVariant replaceAllBool - 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 aArray - 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 aArray - Source:
Returns:
- Type
- Array