Predefined functions for array
pop
Removes the last element from an array and returns it.
If the array is empty, null
is returned and the array is not modified.
Parameters
None
Returns
number
- the removed element from the array if the array was not empty, null
otherwise.
Examples
node some_node { do { var arr: number[] = [1,2]; var el1 = arr.pop(); #log(el1); // 2 #log(arr.pop()); // 1 #log(arr.pop()); // null } }
shift
Removes the first element from an array and returns that element.
If the array is empty, null
is returned and the array is not modified.
Parameters
None
Returns
number
- first element of the array if the array is not empty, null
otherwise.
Examples
node some_node { do { var arr: number[] = [1,2]; var el1 = arr.shift(); #log(el1); // 1 #log(arr.shift()); // 2 #log(arr.shift()); // null } }
push
Appends new element to the end of an array.
Note:
- New element must be of a type compatible to a type of an array.
For example, you can add an element of type
string
or"some_string_literal"
to an array of typestring[]
. But you can not add an element of typenumber
to an array of typestring[]
.
Parameters
Name | Type | Description |
---|---|---|
value | Type compatible with a type of the array | the value of the element to add |
Returns
empty
Examples
node some_node { do { var arr: number[] = [1,2]; arr.push(3); #log(arr); // [1,2,3] arr.push("s"); // compile error } }
unshift
Prepends new element to the beginning of an array.
Note:
- New element must be of a type compatible to a type of an array.
For example, you can add an element of a type
string
or"some_string_literal"
to an array of a typestring[]
. But you can not add an element of typenumber
to an array of a typestring[]
.
Parameters
Name | Type | Description |
---|---|---|
value | Type compatible with a type of an array | the value of the element to add |
Returns
empty
Examples
node some_node { do { var arr: number[] = [1,2]; arr.unshift(3); #log(arr); // [3,1,2] arr.unshift("s"); // compile error } }
append
Appends new elements to the end of an array.
Note:
- The element to be added must be of a type compatible to a type of an array.
For example, you can add elements of type
string[]
or"some_string_literal"[]
to an array of typestring[]
. But you can not add elements of typenumber[]
to an array of typestring[]
.
Parameters
Name | Type | Description |
---|---|---|
values | Type compatible with type of array | the values of the elements to append |
Returns
empty
Examples
node some_node { do { var arr: number[] = [1,2]; arr.append([3,4,5]); #log(arr); // [1,2,3,4,5] arr.append(["s"]); // compile error } }
prepend
Prepends new elements to the beginning of an array.
Note:
The element to be added must be of a type compatible to a type of an array. For example, you can add elements of type
string[]
or"some_string_literal"[]
to an array of typestring[]
. But you can not add elements of typenumber[]
to an array of typestring[]
.Parameters
Name Type Description values Type compatible with type of array the values of the elements to append
Returns
empty
Examples
node some_node { do { var arr: number[] = [1,2]; arr.prepend([3,4,5]); #log(arr); // [3,4,5,1,2] arr.prepend(["s"]); // compile error } }
length
Returns the length of an array.
Parameters
None
Returns
number
- length of an array.
Examples
node some_node { do { var arr: number[] = []; #log(arr.length()); // 0 arr.append([1,2]); arr.prepend([3,4,5]); #log(arr.length()); // 5 arr.pop(); #log(arr.length()); // 4 arr.shift(); #log(arr.length()); // 3 } }
reverse
Returns a new array with the elements of the source in reversed order. The elements of reversed array are the same references as elements of the source array.
The source array stays unmodified.
Parameters
None
Returns
A new array of the same type with elements of the source in reversed order.
Examples
node some_node { do { var arr: number[] = [1,2,3]; var revArr = arr.reverse(); #log(revArr); // [3,2,1] #log(arr); // [1,2,3] } }
remove
Removes elements in range [start
, start+deleteCount
) from an array and returns deleted elemenets.
A negative value of start index means an offset from the end of the array, i.e. arr.remove(-2)
is equivalent for arr.remove(arr.length() - 2)
.
If deleteCount
is omitted, or if deleteCount >= array.length() - start
, then all the elements from start to the end of the array will be deleted.
Note:
- the float value of the
start
index will be truncated to integer value.
Parameters
Name | Type | Description |
---|---|---|
start | number | integer index of the first removed element |
deleteCount | number? | number of elements to remove (not including the element at index start + deleteCount ) |
Returns
An array containing the removed elements.
Examples
node some_node { do { var arr: number[] = [1,2,3,4,5,6]; #log(arr.remove(3,2)); // [4,5] #log(arr); // [1,2,3,6] set arr = [1,2,3,4,5,6]; #log(arr.remove(3,100)); // [4,5,6] #log(arr); // [1,2,3] set arr = [1,2,3,4,5,6]; #log(arr.remove(3)); // [4,5,6] #log(arr); // [1,2,3] set arr = [1,2,3,4,5,6]; #log(arr.remove(100)); // [] #log(arr); // [1,2,3,4,5,6] set arr = [1,2,3,4,5,6]; #log(arr.remove(2, 0)); // [] #log(arr); // [1,2,3,4,5,6] set arr = [1,2,3,4,5,6]; // remove 2 last elements #log(arr.remove(-2)); // [5,6] #log(arr); // [1,2,3,4] set arr = [1,2,3,4,5,6]; // 1.7 is truncated to 1 #log(arr.remove(1.7)); // [2,3,4,5,6] #log(arr); // [1] } }
slice
Returns a copy of a section extracted from an array. Input parameters start
and end
define the section of the array as the range [start
,end
).
A negative value of an index (start
or end
) means an offset from the end of the array (please, see the examples).
If start
is omitted than slice
will extract elements starting from the index 0
.
If end
is omitted or end >= array.length()
than slice
will extract elements through to the end of array.
Note:
- the float value of an index will be truncated to integer value.
Parameters
Name | Type | Description |
---|---|---|
start | number? | integer index at which to start the extraction |
end | number? | integer index at which to end the extraction (not including) |
Returns
A new array containing the extracted elements.
Examples
node some_node { do { var arr: number[] = [1,2,3,4,5,6]; #log(arr.slice()); // [1,2,3,4,5,6] #log(arr.slice(2,4)); // [3,4] #log(arr.slice(2)); // [3,4,5,6] #log(arr.slice(-2)); // [5,6] // 2.9 is truncated to 2 #log(arr.slice(2.9)); // [3,4,5,6] #log(arr.slice(2,10)); // [3,4,5,6] } }
join
Converts all the elements of an array into a string, separated by the specified separator
string.
If separator
omitted, the array elements are separated with a comma (",").
If separator
is an empty string, all elements are joined without any characters in between them.
Parameters
Name | Type | Description |
---|---|---|
separator | string? | string to separate each pair of adjacent elements of the array |
Returns
string
- a string with all array elements joined.
Examples
node some_node { do { var numArr: number[] = [1,2,3,4,5,6]; #log(numArr.join()); // "1,2,3,4,5,6" #log(numArr.join("-")); // "1-2-3-4-5-6" #log(numArr.join("")); // "123456" var strArr: string[] = ["h","e","l","l","o"]; #log(strArr.join("")); // "hello" var boolArr: boolean[] = [true,false]; #log(boolArr.join()); // "true,false" // objects should be stringified manually var objArr: unknown[] = [{a: 1}, {b:2}]; #log(objArr.join()); // "[object Object],[object Object]" } }