# Array
A randomly accessible sequence of values of a generic type.
The Array API is very similar to JavaScript's (MDN (opens new window)), with the notable difference that one must make sure that there are no null values if T is a non-nullable reference type. Example:
var arr = new Array<string>(10)
// arr[0]; // would error 😢
for (let i = 0; i < arr.length; ++i) {
arr[i] = ""
}
arr[0]; // now it works 😊
# Constructor
- Constructs a new array.
new Array<T>(capacity?: i32)
# Static members
- Tests if a value is an array.
function isArray<U>(value: U): bool
# Instance members
# Fields
- The length of this array. Setting the length to a value larger than internal capacity will automatically grow the array.
var length: i32
# Methods
function concat(other: Array<T>): Array<T>Concatenates the values of this and the other array to a new array, in this order.
function copyWithin(target: i32, start: i32, end?: i32): thisCopies a region of an array's values over the respective values starting at the target location.
function every(fn: (value: T, index: i32, self: Array<T>) => bool): boolCalls the specified function with every value of the array until it finds the first value for which the function returns
false. Returnstrueif all functions returnedtrueor the array is empty, otherwisefalse.function fill(value: T, start?: i32, end?: i32): thisReplaces the values of the array from
startinclusive toendexclusive in place with the specified value, returning the array.function filter(fn: (value: T, index: i32, self: Array<T>) => bool): Array<T>Calls the specified function with every value of the array, returning a new array with all values for which the function returned
true.function findIndex(fn: (value: T, index: i32, self: Array<T>) => bool): i32Calls the specified function with every value of the array until it finds the first value for which the function returns
true, returning its index. Returns-1if that's never the case.function findLastIndex(fn: (value: T, index: i32, self: Array<T>) => bool): i32;Calls the specified function with every value of the array starting at the end until it finds the first value for which the function returns
true, returning its index. Returns-1if that's never the case.function flat(): valueof<T>[]Flattens an array of arrays to a one-dimensional array.
nullentries are ignored.function forEach(fn: (value: T, index: i32, self: Array<T>) => void): voidCalls the specified function with every value of the array.
function includes(value: T, fromIndex?: i32): boolTests if the array includes the specified value, optionally providing a starting index.
function indexOf(value: T, fromIndex?: i32): i32Gets the first index where the specified value can be found in the array. Returns
-1if not found.function join(separator?: string): stringConcatenates all values of the array to a string, separated by the specified separator (default:
,).function lastIndexOf(value: T, fromIndex?: i32): i32Gets the last index where the specified value can be found in the array. Returns
-1if not found.function map<U>(fn: (value: T, index: i32, self: Array<T>) => U): Array<U>Calls the specified function with every value of the array, returning a new array of the function's return values.
function pop(): TRemoves and returns the last value of the array. Modifies
Array#length. Throws aRangeErrorif the array is empty.function push(value: T): i32Adds one more value to the end of the array and returns the Array's new length. Modifies
Array#length.function reduce<U>( fn: (accumValue: U, currentValue: T, index: i32, self: Array<T>) => U, initialValue: U ): UCalls the specified reducer function with each value of the array, resulting in a single return value. The respective previous reducer function's return value is remembered in
accumValue, starting withinitialValue, becoming the final return value in the process.function reduceRight<U>( fn: (accumValue: U, currentValue: T, index: i32, self: Array<T>) => U, initialValue: U ): UCalls the specified reducer function with each value of the array, from right to left, resulting in a single return value. See
Array#reducefor the reducer function's signature.function reverse(): thisReverses an array's values in place, modifying the array before returning it.
function shift(): TRemoves and returns the first value of the array. Modifies
Array#length.function slice(start?: i32, end?: i32): Array<T>Returns a shallow copy of the array's values from
begininclusive toendexclusive, as a new array. If omitted,enddefaults to the end of the array.function some(fn: (value: T, index: i32, self: Array<T>) => bool): boolCalls the specified function with every value of the array until it finds the first value for which the function returns
true, returningtrue. Returnsfalseotherwise or if the array is empty.function sort(fn?: (a: T, b: T) => i32): thisSorts the values of the array in place, using the specified comparator function, modifying the array before returning it. The comparator returning a negative value means
a < b, a positive value meansa > band0means that both are equal. Unlike in JavaScript, where an implicit conversion to strings is performed, the comparator defaults to compare two values of typeT.function splice(start: i32, deleteCount?: i32): Array<T>Removes
deleteCount(defaults to all remaining) values from the array, starting at indexstart, modifying the array in place, returning the removed values.function toString(): stringReturns the result of
Array#join().function unshift(value: T): i32Adds one more value to the start of the array and returns the Array's new length. Modifies
Array#length.
← Globals ArrayBuffer →