Array Method Reference
concat
Section titled “concat”Returns a new array consisting of the elements of the two provided arrays.
var arr1 = [1, 2, 3];var arr2 = [4, 5, 6];var combined_arr = arr1.concat(arr2); // [1, 2, 3, 4, 5, 6]Parameters
Section titled “Parameters”other: [dyn!] - The other array to combine with.
Returns
Section titled “Returns”[dyn!] - A new array containing the elements of both arrays.
contains
Section titled “contains”Returns the first index of a specified value.
var arr = [1, 2, 3];var first = arr.contains(2); // truevar first = arr.index_of(4); // falseParameters
Section titled “Parameters”index: dyn! - The value to check the array for.
Returns
Section titled “Returns”Boolean - true if the item is in the array, otherwise false.
Returns a value at the specified index.
var arr = [1, 2, 3];var first = arr.get(0); // 1var last = arr.get(-1); // 3var out_of_bounds = arr.get(3); // nil
var maybe_array = nil;var maybe_first = maybe_array?.get(0); // nilParameters
Section titled “Parameters”index: Number - The specified index of the item to get. If negative, counts from the end of the array.
Returns
Section titled “Returns”dyn! - The item at the index specified index, nil if the index is out-of-bounds.
index_of
Section titled “index_of”Returns the first index of a specified value.
var arr = [1, 2, 3];var first = arr.index_of(2); // 1var first = arr.index_of(4); // -1Parameters
Section titled “Parameters”index: dyn! - The value to find the index of.
Returns
Section titled “Returns”Number - The index of the value, or -1 if not found.
Creates an Iterator for the array.
var arr = [1, 2, 3];var count = arr.iter().count(); // 3Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”ArrayIterator - An iterator representing the array.
Returns a value at the specified index.
var chars = ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", "!"];var string = chars.join(); // "Hello World!"
var list = [1, 2, 3];var list_as_string = list.join(","); // "1,2,3"Parameters
Section titled “Parameters”delimter: String? optional - The delimeter to use when joining the strings together. If none is provided, "" is used.
Returns
Section titled “Returns”String - Each item converted to a string and joined together, separated by the delimiter if provided.
length
Section titled “length”Returns the length of the array.
var length = [].length(); // 0Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”Number - Length of the array.
Removes the last item of the array, mutating it and returns it.
var last = [1].pop(); // 1Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”dyn! - The last element of the array. nil if the array is empty.
Adds a new item to the end of the array, mutating it.
var arr = [];arr.push(1); // [1]Parameters
Section titled “Parameters”item: dyn! - The item to add to the array.
Returns
Section titled “Returns”dyn! - The last element of the array.
remove_at
Section titled “remove_at”Removes the item at the specified index and returns it.
var arr = [1, 2, 3];var two = arr.remove_at(1); // 2println(arr); // [1, 3]Parameters
Section titled “Parameters”index: Number - The specified index of the item to remove. If negative, counts from the end of the array.
Returns
Section titled “Returns”dyn! - The item at the index specified index, nil if the index is out-of-bounds.
reverse
Section titled “reverse”Reverses the array in place.
var arr = [1, 2, 3];arr.reverse(); // [3, 2, 1]Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”nil
Returns a shallow copy of a portion of the array.
var arr = [1, 2, 3, 4, 5];var slice1 = arr.slice(1); // [2, 3, 4, 5]var slice2 = arr.slice(-2); // [4, 5]var slice3 = arr.slice(1, 3); // [2, 3]var slice4 = arr.slice(1, -1); // [2, 3, 4]Parameters
Section titled “Parameters”start: Number? optional - The beginning index (inclusive). Negative indices count from the end. If omitted, defaults to 0. Values beyond array bounds are clamped.
end: Number? optional - The ending index (exclusive). Negative indices count from the end. If omitted, slices to the end of the array. Values beyond array bounds are clamped.
Returns
Section titled “Returns”[dyn!] - A shallow copy of the array between start (inclusive) and end (exclusive). Returns an empty array if start >= end.