goog.array
Exported Functions
binaryInsert<VALUE>( array, value, opt_compareFn ) → boolean
boolean
Inserts a value into a sorted array. The array is not modified if the value is already present.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
binaryRemove<VALUE>( array, value, opt_compareFn ) → boolean
boolean
Removes a value from a sorted array.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
binarySearch<TARGET, VALUE>( arr, target, opt_compareFn ) → number
number
Searches the specified array for the specified target using the binary
search algorithm. If no opt_compareFn is specified, elements are compared
using defaultCompare
, which compares the elements
using the built in < and > operators. This will produce the expected
behavior for homogeneous arrays of String(s) and Number(s). The array
specified must be sorted in ascending order (as defined by the
comparison function). If the array is not sorted, results are undefined.
If the array contains multiple instances of the specified target value, the
left-most instance will be found.
Runtime: O(log n)
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
binarySelect<THIS, VALUE>( arr, evaluator, opt_obj ) → number
number
Selects an index in the specified array using the binary search algorithm. The evaluator receives an element and determines whether the desired index is before, at, or after it. The evaluator must be consistent (formally, map(map(arr, evaluator, opt_obj), goog.math.sign) must be monotonically non-increasing).
Runtime: O(log n)
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
Splits an array into disjoint buckets according to a splitting function.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
Splits an array into disjoint buckets according to a splitting function.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
clear( arr ) → void
void
clone<T>( arr ) → Array<(T|null)>
Array<(T|null)>
Does a shallow copy of an array.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
compare3<VALUE>( arr1, arr2, opt_compareFn ) → number
number
3-way array compare function.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
concat( ...var_args ) → Array<?>
Array<?>
Returns a new array that is the result of joining the arguments. If arrays are passed then their items are added, however, if non-arrays are passed they will be added to the return array as is.
Note that ArrayLike objects will be added as is, rather than having their items added.
concat([1, 2], [3, 4]) -> [1, 2, 3, 4] concat(0, [1, 2]) -> [0, 1, 2] concat([1, 2], null) -> [1, 2, null]
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
concatMap<THIS, VALUE, RESULT>( arr, f, opt_obj ) → Array<(RESULT|null)>
Array<(RESULT|null)>
Maps each element of the input array into zero or more elements of the output array.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
contains( arr, obj ) → boolean
boolean
Whether the array contains the given object.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
copyByIndex<T>( arr, index_arr ) → Array<(T|null)>
Array<(T|null)>
Returns a new array of elements from arr, based on the indexes of elements provided by index_arr. For example, the result of index copying ['a', 'b', 'c'] with index_arr [1,0,0,2] is ['b', 'a', 'a', 'c'].
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
count<T, S>( arr, f, opt_obj ) → number
number
Counts the array elements that fulfill the predicate, i.e. for which the callback function returns true. Skips holes in the array.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
defaultCompare<VALUE>( a, b ) → number
number
Compares its two arguments for order, using the built in < and > operators.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
defaultCompareEquality( a, b ) → boolean
boolean
Compares its two arguments for equality, using the built in === operator.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
equals<A, B>( arr1, arr2, opt_equalsFn ) → boolean
boolean
Compares two arrays for equality. Two arrays are considered equal if they have the same length and their corresponding elements are equal according to the comparison function.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
every<T, S>( arr, f, opt_obj ) → boolean
boolean
Call f for each element of an array. If all calls return true, every() returns true. If any call returns false, every() returns false and does not continue to check the remaining elements.
See http://tinyurl.com/developer-mozilla-org-array-every
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
extend<VALUE>( arr1, ...var_args ) → void
void
Extends an array with another array, element, or "array like" object. This function operates 'in-place', it does not create a new Array.
Example: var a = []; extend(a, [0, 1]); a; // [0, 1] extend(a, 2); a; // [0, 1, 2]
Parameters |
|
---|
filter<T, S>( arr, f, opt_obj ) → Array<(T|null)>
Array<(T|null)>
Calls a function for each element in an array, and if the function returns true adds the element to a new array.
See http://tinyurl.com/developer-mozilla-org-array-filter
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
find<T, S>( arr, f, opt_obj ) → ?
?
Search an array for the first element that satisfies a given condition and return that element.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
findIndex<T, S>( arr, f, opt_obj ) → number
number
Search an array for the first element that satisfies a given condition and return its index.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
findIndexRight<T, S>( arr, f, opt_obj ) → number
number
Search an array (in reverse order) for the last element that satisfies a given condition and return its index.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
findRight<T, S>( arr, f, opt_obj ) → ?
?
Search an array (in reverse order) for the last element that satisfies a given condition and return that element.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
flatten( ...var_args ) → Array<?>
Array<?>
Returns an array consisting of every argument with all arrays expanded in-place recursively.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
forEach<T, S>( arr, f, opt_obj ) → void
void
Calls a function for each element in an array. Skips holes in the array.
See http://tinyurl.com/developer-mozilla-org-array-foreach
Parameters |
|
---|
forEachRight<T, S>( arr, f, opt_obj ) → void
void
Calls a function for each element in an array, starting from the last element rather than the first.
Parameters |
|
---|
indexOf<T>( arr, obj, opt_fromIndex ) → number
number
Returns the index of the first element of an array with a specified value, or -1 if the element is not present in the array.
See http://tinyurl.com/developer-mozilla-org-array-indexof
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
insert<T>( arr, obj ) → void
void
Pushes an item into an array, if it's not already in the array.
Parameters |
|
---|
insertArrayAt( arr, elementsToAdd, opt_i ) → void
void
Inserts at the given index of the array, all elements of another array.
Parameters |
|
---|
insertAt( arr, obj, opt_i ) → void
void
Inserts an object at the given index of the array.
Parameters |
|
---|
insertBefore<T>( arr, obj, opt_obj2 ) → void
void
Inserts an object into an array before a specified object.
Parameters |
|
---|
inverseDefaultCompare<VALUE>( a, b ) → number
number
Compares its two arguments for inverse order, using the built in < and > operators.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
isEmpty( arr ) → boolean
boolean
Whether the array is empty.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
isSorted<T>( arr, opt_compareFn, opt_strict ) → boolean
boolean
Tells if the array is sorted.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
join<T>( ...var_args ) → Array<(T|null)>
Array<(T|null)>
last<T>( array ) → T
T
Returns the last element in an array without removing it.
Same as goog.array.peek
.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
lastIndexOf<T>( arr, obj, opt_fromIndex ) → number
number
Returns the index of the last element of an array with a specified value, or -1 if the element is not present in the array.
See http://tinyurl.com/developer-mozilla-org-array-lastindexof
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
map<THIS, VALUE, RESULT>( arr, f, opt_obj ) → Array<(RESULT|null)>
Array<(RESULT|null)>
Calls a function for each element in an array and inserts the result into a new array.
See http://tinyurl.com/developer-mozilla-org-array-map
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
moveItem( arr, fromIndex, toIndex ) → void
void
Moves one item of an array to a new position keeping the order of the rest of the items. Example use case: keeping a list of JavaScript objects synchronized with the corresponding list of DOM elements after one of the elements has been dragged to a new position.
Parameters |
|
---|
peek<T>( array ) → T
T
Returns the last element in an array without removing it.
Same as goog.array.last
.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
Creates a range of numbers in an arithmetic progression.
Range takes 1, 2, or 3 arguments:
range(5) is the same as range(0, 5, 1) and produces [0, 1, 2, 3, 4] range(2, 5) is the same as range(2, 5, 1) and produces [2, 3, 4] range(-2, -5, -1) produces [-2, -3, -4] range(-2, -5, 1) produces [], since stepping by 1 wouldn't ever reach -5.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
reduce<T, S, R>( arr, f, val, opt_obj ) → R
R
Passes every element of an array into a function and accumulates the result.
See http://tinyurl.com/developer-mozilla-org-array-reduce
Note that this implementation differs from the native Array.prototype.reduce
in that the initial value is assumed to be defined (the MDN docs linked above
recommend not omitting this parameter, although it is technically optional).
For example: var a = [1, 2, 3, 4]; reduce(a, function(r, v, i, arr) {return r + v;}, 0); returns 10
Parameters |
| ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
reduceRight<T, S, R>( arr, f, val, opt_obj ) → R
R
Passes every element of an array into a function and accumulates the result, starting from the last element and working towards the first.
See http://tinyurl.com/developer-mozilla-org-array-reduceright
For example: var a = ['a', 'b', 'c']; reduceRight(a, function(r, v, i, arr) {return r + v;}, ''); returns 'cba'
Parameters |
| ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
remove<T>( arr, obj ) → boolean
boolean
Removes the first occurrence of a particular value from an array.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
removeAllIf<T, S>( arr, f, opt_obj ) → number
number
Removes all values that satisfy the given condition.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
removeAt( arr, i ) → boolean
boolean
Removes from an array the element at index i
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
removeDuplicates<T>( arr, opt_rv, opt_hashFn ) → void
void
Removes all duplicates from an array (retaining only the first occurrence of each array element). This function modifies the array in place and doesn't change the order of the non-duplicate items.
For objects, duplicates are identified as having the same unique ID as
defined by goog.getUid
.
Alternatively you can specify a custom hash function that returns a unique value for each item in the array it should consider unique.
Runtime: N, Worstcase space: 2N (no dupes)
Parameters |
|
---|
removeIf<T, S>( arr, f, opt_obj ) → boolean
boolean
Removes the first value that satisfies the given condition.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
removeLast<T>( arr, obj ) → boolean
boolean
Removes the last occurrence of a particular value from an array.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
repeat<VALUE>( value, n ) → Array<(VALUE|null)>
Array<(VALUE|null)>
rotate<T>( array, n ) → Array<(T|null)>
Array<(T|null)>
Rotates an array in-place. After calling this method, the element at index i will be the element previously at index (i - n) % array.length, for all values of i between 0 and array.length - 1, inclusive.
For example, suppose list comprises [t, a, n, k, s]. After invoking rotate(array, 1) (or rotate(array, -4)), array will comprise [s, t, a, n, k].
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
shuffle( arr, opt_randFn ) → void
void
Shuffles the values in the specified array using the Fisher-Yates in-place shuffle (also known as the Knuth Shuffle). By default, calls Math.random() and so resets the state of that random number generator. Similarly, may reset the state of any other specified random number generator.
Runtime: O(n)
Parameters |
|
---|
slice<T>( arr, start, opt_end ) → Array<(T|null)>
Array<(T|null)>
Returns a new array from a segment of an array. This is a generic version of Array slice. This means that it might work on other objects similar to arrays, such as the arguments object.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
some<T, S>( arr, f, opt_obj ) → boolean
boolean
Calls f for each element of an array. If any call returns true, some() returns true (without checking the remaining elements). If all calls return false, some() returns false.
See http://tinyurl.com/developer-mozilla-org-array-some
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
sort<T>( arr, opt_compareFn ) → void
void
Sorts the specified array into ascending order. If no opt_compareFn is
specified, elements are compared using
defaultCompare
, which compares the elements using
the built in < and > operators. This will produce the expected behavior
for homogeneous arrays of String(s) and Number(s), unlike the native sort,
but will give unpredictable results for heterogeneous lists of strings and
numbers with different numbers of digits.
This sort is not guaranteed to be stable.
Runtime: Same as Array.prototype.sort
Parameters |
|
---|
sortByKey<T, K>( arr, keyFn, opt_compareFn ) → void
void
Sort the specified array into ascending order based on item keys
returned by the specified key function.
If no opt_compareFn is specified, the keys are compared in ascending order
using defaultCompare
.
Runtime: O(S(f(n)), where S is runtime of sort
and f(n) is runtime of the key function.
Parameters |
|
---|
sortObjectsByKey( arr, key, opt_compareFn ) → void
void
Sorts an array of objects by the specified object key and compare
function. If no compare function is provided, the key values are
compared in ascending order using defaultCompare
.
This won't work for keys that get renamed by the compiler. So use
{'foo': 1, 'bar': 2} rather than {foo: 1, bar: 2}.
Parameters |
|
---|
splice<T>( arr, index, howMany, ...var_args ) → Array<(T|null)>
Array<(T|null)>
Adds or removes elements from an array. This is a generic version of Array splice. This means that it might work on other objects similar to arrays, such as the arguments object.
Parameters |
| ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
stableSort<T>( arr, opt_compareFn ) → void
void
Sorts the specified array into ascending order in a stable way. If no
opt_compareFn is specified, elements are compared using
defaultCompare
, which compares the elements using
the built in < and > operators. This will produce the expected behavior
for homogeneous arrays of String(s) and Number(s).
Runtime: Same as Array.prototype.sort
, plus an additional
O(n) overhead of copying the array twice.
Parameters |
|
---|
toArray<T>( object ) → Array<(T|null)>
Array<(T|null)>
Converts an object to an array.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
toMap<K, V>( arr, keyFunc ) → Map<(K|null), (V|null)>
Map<(K|null), (V|null)>
Creates a new ES6 Map built from the provided array and the key-generation function.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
toObject<T, S>( arr, keyFunc, opt_obj ) → Object<?, (T|null)>
Object<?, (T|null)>
Creates a new object built from the provided array and the key-generation function.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
Creates a new array for which the element at position i is an array of the ith element of the provided arrays. The returned array will only be as long as the shortest array provided; additional values are ignored. For example, the result of zipping [1, 2] and [3, 4, 5] is [[1,3], [2, 4]].
This is similar to the zip() function in Python. See http://docs.python.org/library/functions.html#zip
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
Compiler Constants
goog.array.ASSUME_NATIVE_FUNCTIONS → boolean
boolean
If true, JSCompiler will use the native implementation of
array functions where appropriate (e.g., Array#filter
) and remove the
unused pure JS implementation.