goog.array
Namespace.ArrayLike
.binaryInsert(array, value, opt_compareFn)
Inserts a value into a sorted array. The array is not modified if the value is already present.
array
{Array
.<T
>}
value
{T
}
opt_compareFn
{?function
(T
,T
):number
=}
boolean
}
.binaryRemove(array, value, opt_compareFn)
Removes a value from a sorted array.
array
{Array
}
value
{*}
opt_compareFn
{Function
=}
boolean
}
.binarySearch(arr, target, opt_compareFn)
Searches the specified array for the specified target using the binary
search algorithm. If no opt_compareFn is specified, elements are compared
using goog.array.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, any
of these instances may be found.
Runtime: O(log n)
arr
{goog.array.ArrayLike
}
target
{*}
opt_compareFn
{Function
=}
number
}
.binarySelect(arr, evaluator, opt_obj)
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, goog.array.map(goog.array.map(arr, evaluator, opt_obj), goog.math.sign) must be monotonically non-increasing). Runtime: O(log n)
arr
{goog.array.ArrayLike
}
evaluator
{Function
}
opt_obj
{Object
=}
number
}
.bucket(array, sorter)
Splits an array into disjoint buckets according to a splitting function.
array
{Array
.<T
>}
sorter
{function
(T
,number
,Array
.<T
>):?}
Object
}
.clear(arr)
Clears the array.
arr
{goog.array.ArrayLike
}
.clone
Does a shallow copy of an array.
arr
{goog.array.ArrayLike
}
Array
}
.compare(arr1, arr2, opt_equalsFn)
[deprecated]Deprecated. Use {@link goog.array.equals}.
arr1
{goog.array.ArrayLike
}
arr2
{goog.array.ArrayLike
}
opt_equalsFn
{Function
=}
boolean
}
.compare3(arr1, arr2, opt_compareFn)
3-way array compare function.
arr1
{!goog.array.ArrayLike
}
arr2
{!goog.array.ArrayLike
}
opt_compareFn
{?function
(?, ?): number
=}
number
}
.concat(var_args)
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. goog.array.concat([1, 2], [3, 4]) -> [1, 2, 3, 4] goog.array.concat(0, [1, 2]) -> [0, 1, 2] goog.array.concat([1, 2], null) -> [1, 2, null] There is bug in all current versions of IE (6, 7 and 8) where arrays created in an iframe become corrupted soon (not immediately) after the iframe is destroyed. This is common if loading data via goog.net.IframeIo, for example. This corruption only affects the concat method which will start throwing Catastrophic Errors (#-2147418113). See http://endoflow.com/scratch/corrupted-arrays.html for a test case. Internally goog.array should use this, so that all methods will continue to work on these broken array objects.
var_args
{...*}
Array
}
.contains(arr, obj)
Whether the array contains the given object.
arr
{goog.array.ArrayLike
}
obj
{*}
boolean
}
.defaultCompare(a, b)
Compares its two arguments for order, using the built in < and > operators.
a
{*}
b
{*}
number
}
.defaultCompareEquality(a, b)
Compares its two arguments for equality, using the built in === operator.
a
{*}
b
{*}
boolean
}
.equals(arr1, arr2, opt_equalsFn)
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.
arr1
{goog.array.ArrayLike
}
arr2
{goog.array.ArrayLike
}
opt_equalsFn
{Function
=}
boolean
}
.every
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 {@link http://tinyurl.com/developer-mozilla-org-array-every}
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?) : boolean
}
opt_obj
{S
=}
boolean
}
.extend(arr1, var_args)
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 = []; goog.array.extend(a, [0, 1]); a; // [0, 1] goog.array.extend(a, 2); a; // [0, 1, 2]
arr1
{Array
}
var_args
{...*}
.filter
Calls a function for each element in an array, and if the function returns true adds the element to a new array. See {@link http://tinyurl.com/developer-mozilla-org-array-filter}
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?):boolean
}
opt_obj
{S
=}
Array
}
.find(arr, f, opt_obj)
Search an array for the first element that satisfies a given condition and return that element.
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?) : boolean
}
opt_obj
{S
=}
T
}
.findIndex(arr, f, opt_obj)
Search an array for the first element that satisfies a given condition and return its index.
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?) : boolean
}
opt_obj
{S
=}
number
}
.findIndexRight(arr, f, opt_obj)
Search an array (in reverse order) for the last element that satisfies a given condition and return its index.
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?) : boolean
}
opt_obj
{Object
=}
number
}
.findRight(arr, f, opt_obj)
Search an array (in reverse order) for the last element that satisfies a given condition and return that element.
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?) : boolean
}
opt_obj
{S
=}
T
}
.flatten(var_args)
Returns an array consisting of every argument with all arrays expanded in-place recursively.
var_args
{...*}
Array
}
.forEach
Calls a function for each element in an array. See {@link http://tinyurl.com/developer-mozilla-org-array-foreach}
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
: S
, T
, number
, ?): ?}
opt_obj
{S
=}
.forEachRight(arr, f, opt_obj)
Calls a function for each element in an array, starting from the last element rather than the first.
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
: S
, T
, number
, ?): ?}
opt_obj
{S
=}
.indexOf
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 {@link http://tinyurl.com/developer-mozilla-org-array-indexof}
arr
{goog.array.ArrayLike
}
obj
{*}
opt_fromIndex
{number
=}
number
}
.insert(arr, obj)
Pushes an item into an array, if it's not already in the array.
arr
{Array
.<T
>}
obj
{T
}
.insertArrayAt(arr, elementsToAdd, opt_i)
Inserts at the given index of the array, all elements of another array.
arr
{goog.array.ArrayLike
}
elementsToAdd
{goog.array.ArrayLike
}
opt_i
{number
=}
.insertAt(arr, obj, opt_i)
Inserts an object at the given index of the array.
arr
{goog.array.ArrayLike
}
obj
{*}
opt_i
{number
=}
.insertBefore(arr, obj, opt_obj2)
Inserts an object into an array before a specified object.
arr
{Array
.<T
>}
obj
{T
}
opt_obj2
{T
=}
.isEmpty(arr)
Whether the array is empty.
arr
{goog.array.ArrayLike
}
boolean
}
.isSorted(arr, opt_compareFn, opt_strict)
Tells if the array is sorted.
arr
{!Array
.<T
>}
opt_compareFn
{?function
(T
,T
):number
=}
opt_strict
{boolean
=}
boolean
}
.lastIndexOf
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 {@link http://tinyurl.com/developer-mozilla-org-array-lastindexof}
arr
{goog.array.ArrayLike
}
obj
{*}
opt_fromIndex
{?number
=}
number
}
.map
Calls a function for each element in an array and inserts the result into a new array. See {@link http://tinyurl.com/developer-mozilla-org-array-map}
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?):?}
opt_obj
{S
=}
Array
}
.peek(array)
Returns the last element in an array without removing it.
array
{goog.array.ArrayLike
}
.reduce(arr, f, val, opt_obj)
Passes every element of an array into a function and accumulates the result. See {@link http://tinyurl.com/developer-mozilla-org-array-reduce} For example: var a = [1, 2, 3, 4]; goog.array.reduce(a, function(r, v, i, arr) {return r + v;}, 0); returns 10
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, R
, T
, number
, ?) : R
}
val
{?}
opt_obj
{S
=}
R
}
.reduceRight(arr, f, val, opt_obj)
Passes every element of an array into a function and accumulates the result, starting from the last element and working towards the first. See {@link http://tinyurl.com/developer-mozilla-org-array-reduceright} For example: var a = ['a', 'b', 'c']; goog.array.reduceRight(a, function(r, v, i, arr) {return r + v;}, ''); returns 'cba'
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, R
, T
, number
, ?) : R
}
val
{?}
opt_obj
{S
=}
R
}
.remove(arr, obj)
Removes the first occurrence of a particular value from an array.
arr
{goog.array.ArrayLike
}
obj
{*}
boolean
}
.removeAt(arr, i)
Removes from an array the element at index i
arr
{goog.array.ArrayLike
}
i
{number
}
boolean
}
.removeDuplicates(arr, opt_rv)
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 {@link goog.getUid}. Runtime: N, Worstcase space: 2N (no dupes)
arr
{goog.array.ArrayLike
}
opt_rv
{Array
=}
.removeIf(arr, f, opt_obj)
Removes the first value that satisfies the given condition.
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?) : boolean
}
opt_obj
{S
=}
boolean
}
.repeat(value, n)
Returns an array consisting of the given value repeated N times.
value
{*}
n
{number
}
Array
}
.rotate(array, n)
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].
array
{!Array
.<T
>}
n
{number
}
Array
.<T
>}
.shuffle(arr, opt_randFn)
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 the any other specified random number generator. Runtime: O(n)
arr
{!Array
}
opt_randFn
{function
():number
=}
.slice(arr, start, opt_end)
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.
arr
{Array
.<T
>|goog.array.ArrayLike
}
start
{number
}
opt_end
{number
=}
Array
.<T
>}
.some
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 {@link http://tinyurl.com/developer-mozilla-org-array-some}
arr
{Array
.<T
>|goog.array.ArrayLike
}
f
{?function
(this
:S
, T
, number
, ?) : boolean
}
opt_obj
{S
=}
boolean
}
.sort(arr, opt_compareFn)
Sorts the specified array into ascending order. If no opt_compareFn is
specified, elements are compared using
goog.array.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 heterogenous lists of strings and
numbers with different numbers of digits.
This sort is not guaranteed to be stable.
Runtime: Same as Array.prototype.sort
arr
{Array
.<T
>}
opt_compareFn
{?function
(T
,T
):number
=}
.sortObjectsByKey(arr, key, opt_compareFn)
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 goog.array.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}.
arr
{Array
.<Object
>}
key
{string
}
opt_compareFn
{Function
=}
.splice(arr, index, howMany, var_args)
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.
arr
{goog.array.ArrayLike
}
index
{number
|undefined
}
howMany
{number
}
var_args
{...*}
Array
}
.stableSort(arr, opt_compareFn)
Sorts the specified array into ascending order in a stable way. If no
opt_compareFn is specified, elements are compared using
goog.array.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.
arr
{Array
.<T
>}
opt_compareFn
{?function
(T
, T
): number
=}
.toArray(object)
Converts an object to an array.
object
{goog.array.ArrayLike
}
Array
}
.toObject(arr, keyFunc, opt_obj)
Creates a new object built from the provided array and the key-generation function.
arr
{Array
.<T
>|goog.array.ArrayLike
}
keyFunc
{?function
(this
:S
, T
, number
, ?) : string
}
opt_obj
{S
=}
Object
.<T
>}
.zip(var_args)
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 {@link http://docs.python.org/library/functions.html#zip}
var_args
{...!goog.array.ArrayLike
}
Array
.<!Array
>}