Skip to content
hhh edited this page Jul 18, 2022 · 9 revisions

Array

__invoke

Array.__invoke(data...)

Returns an array containing the given data as elements.

create

Array.create(size = 0, init = null)

Returns a newly-created array. You can optionally specify a size and an initial value to fill the array.

clone

Array.clone(array)

Returns a copy of the given array.

sizeOf

Array.sizeOf(array)

Returns the size of the given array.

push

Array.push(array, data...)

Add some data to the end of the array.

unshift

Array.unshift(array, data...)

Add some data to the start of the array.

pop

Array.pop(array)

Removes the last element of the array and returns it.

shift

Array.shift(array)

Removes the first element of the array and returns it.

slice

Array.slice(array, start = 0, end = Array.sizeOf(array)))

Returns a slice of the given array. You can optionally specify start index and end index.

insert

Array.insert(array, index, data...)

Inserts some data at the given index in the array.

remove

Array.remove(array, index, count = 1)

Removes some elements at the given index in the array.

clear

Array.clear(array)

Empties the given array.

flat

Array.flat(arrays, depth = 1)

Flatten the given array of arrays. You can optionally specify the flattening depth.

reverse

Array.reverse(array)

Reverse the given array in place and returns array itself.

unpack

Array.unpack(array, names, loose = false)

Unpacks the elements in the given array and stores them in variables specified by names. When option loose is false(default), errors will be raised if there are more names than available values; otherwise, excess names result in null values.

indexOf

Array.indexOf(array, value)

Returns the index where the given value occurs in the given array for the first time. Returns -1 if the value never occurs.

lastIndexOf

Array.lastIndexOf(array, value)

Returns the index where the given value occurs in the given array for the last time. Returns -1 if the value never occurs.

includes

Array.includes(array, value)

Returns a boolean telling whether the given array includes specific value.

sort

Array.sort(array, compareFn?)

Sorts the given array according to the given comparing function. Two elements are swapped when the comparing function returns a positive value. The default comparing function sorts elements in ascending order. (Using Array.prototype.sort in JavaScript source code.)

forEach

Array.forEach(array, callback)

Iterates array and invokes callback on each element. The callback is provided with 3 arguments: element, index and array.

map

Array.map(array, callback)

Maps array to a new one, where new elements are returned by callback. (The original array won't be modified.) The callback is provided with 3 arguments: element, index and array.

For example,

a = [1, 2, 3];
b = a:map((x) => (2 ** x));
"b becomes [2, 4, 8] here.";

filter

Array.filter(array, callback)

Filters array and returns a new array representing the result. (Elements are kept only when callback returns true; The original array won't be modified.) The callback is provided with 3 arguments: element, index and array.

For example,

a = [1, 2, 3];
b = a:filter((x) => ((x & 1) === 1));   "odd";
"b becomes [1, 3] here.";

every

Array.every(array, callback)

Returns false if callback returns false for any element in array; returns true if callback returns true for all elements or array is empty. The callback is provided with 3 arguments: element, index and array.

For example,

a = [1, 2, 3];
allPositive = a:every((x) => (x > 0));      "true";
allOdd = a:every((x) => ((x & 1) === 1));   "false";

some

Array.some(array, callback)

Returns true if callback returns true for any element in array; returns false if callback returns false for all elements or array is empty. The callback is provided with 3 arguments: element, index and array.

For example,

a = [1, 2, 3];
someOdd = a:some((x) => ((x & 1) === 1));   "true";
someNegative = a:some((x) => (x < 0));      "false";

findIndex

Array.findIndex(array, callback)

Returns the first index where callback returns true; throws an error if callback returns false for all elements. The callback is provided with 3 arguments: element, index and array.

For example,

a = [1, 2, 3];
indexOdd = a:findIndex((x) => ((x & 1) === 1)); "0";
indexNeg = a:findIndex((x) => (x < 0));         "Error!";

find

Array.find(array, callback)

Similar to Array.findIndex, but returns the element instead of the index.

For example,

a = [1, 2, 3];
indexOdd = a:findIndex((x) => ((x & 1) === 1)); "1";
indexNeg = a:findIndex((x) => (x < 0));         "Error!";
Clone this wiki locally