Array Functions
Arrays in Flowata are ordered collections of values, and you can access or modify their elements using index-based accessors. They are created with the []
syntax.
Updating Array Values
To update a value within an array, you can use index-based accessors. Remember, Flowata uses 1-based indexing:
setLocal(simpleArray, ["apple", "banana", "cherry"]);
setLocal(simpleArray[1], "orange"); // Updates the first element of the array
print(simpleArray); // Output: ["orange", "banana", "cherry"]
In the example above, the first element of simpleArray
is updated to "orange".
Length
To find the length (the number of elements) of an array, you use the length
function.
setLocal(myArray, [1, 2, 3, 4, 5]);
print(length(myArray)); // Output: 5
push(array, value)
Adds a value to the end of an array
pop(array)
Removes the last value from an array
shift(array)
Removes the first value from an array
unshift(array, value)
Adds a value to the beginning of an array
removeItemByIndex(array, index)
Removes an array element at the specified index
slice(array, start, end)
Returns a new array that includes elements from start
to end
.
indexOf(array, value, startIndex)
Returns the first index at which a given value can be found in the array, or -1 if it is not present.
lastIndexOf(array, value, startIndex)
Returns the last index at which a given value can be found in the array, or -1 if it is not present.
reverse(array)
Reverses the order of the elements in an array
has(array, value)
Checks if an array contains a certain value and returns true
or false
.
Examples:
print(has([1, 2, 3, 4], 3)); // Output: true
print(has([1, 2, 3, 4], 5)); // Output: false
find(array, value, start?, end?)
Searches for a value within an array and returns the first index at which it can be found. If the value is not found, it returns -1. The search can be limited with optional start and end parameters.
Examples:
print(find([1, 2, 3, 4, 5], 3)); // Output: 3
print(find([1, 2, 3, 4, 5], 3, 4)); // Output: -1
removeItem(array, value)
Removes all instances of a value from an array
Examples:
print(removeItem([1, 2, 3, 2, 4], 2)); // Output: [1, 3, 4]
clone(...arrays)
When provided with a single array, it creates a shallow copy of the array. When provided with multiple arrays, it concatenates them into a single array.
Examples:
- Cloning an array:
setLocal(original, [1, 2, 3, 4]);
setLocal(duplicate, clone(original));
// Pushing a new value to the original array
push(original, 5);
print(original); // Output: [1, 2, 3, 4, 5]
print(duplicate); // Output: [1, 2, 3, 4]
- Concatenating multiple arrays:
setLocal(arr1, [1, 2]);
setLocal(arr2, [3, 4]);
setLocal(result, clone(arr1, arr2));
print(result); // Output: [1, 2, 3, 4]
clearArray(array)
Clears all elements from an array, making it an empty array. This is an in-place operation, meaning the original array reference is modified.
Examples:
-
Clearing an Array
setLocal(myArray, [1, 2, 3, 4]); clearArray(myArray); print(myArray); // Output: []
In the example above, myArray
is cleared, and when printed, it shows an empty array.