Skip to content

Array Method Reference

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]

other: [dyn!] - The other array to combine with.

[dyn!] - A new array containing the elements of both arrays.

Returns the first index of a specified value.

var arr = [1, 2, 3];
var first = arr.contains(2); // true
var first = arr.index_of(4); // false

index: dyn! - The value to check the array for.

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); // 1
var last = arr.get(-1); // 3
var out_of_bounds = arr.get(3); // nil
var maybe_array = nil;
var maybe_first = maybe_array?.get(0); // nil

index: Number - The specified index of the item to get. If negative, counts from the end of the array.

dyn! - The item at the index specified index, nil if the index is out-of-bounds.

Returns the first index of a specified value.

var arr = [1, 2, 3];
var first = arr.index_of(2); // 1
var first = arr.index_of(4); // -1

index: dyn! - The value to find the index of.

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(); // 3

None

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"

delimter: String? optional - The delimeter to use when joining the strings together. If none is provided, "" is used.

String - Each item converted to a string and joined together, separated by the delimiter if provided.

Returns the length of the array.

var length = [].length(); // 0

None

Number - Length of the array.

Removes the last item of the array, mutating it and returns it.

var last = [1].pop(); // 1

None

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]

item: dyn! - The item to add to the array.

dyn! - The last element of the array.

Removes the item at the specified index and returns it.

var arr = [1, 2, 3];
var two = arr.remove_at(1); // 2
println(arr); // [1, 3]

index: Number - The specified index of the item to remove. If negative, counts from the end of the array.

dyn! - The item at the index specified index, nil if the index is out-of-bounds.

Reverses the array in place.

var arr = [1, 2, 3];
arr.reverse(); // [3, 2, 1]

None

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]

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.

[dyn!] - A shallow copy of the array between start (inclusive) and end (exclusive). Returns an empty array if start >= end.