Index Block
Retrieve values from an array at specified indices
Overview
The Index Block allows you to retrieve values from an array at specified indices. It can return a single value when given a single index or multiple values when provided with an array of indices. This block is particularly useful for extracting specific elements from arrays or for reordering array elements based on a set of indices.
Key Features
- Retrieve a single value at a specified index
- Extract multiple values using an array of indices
- Supports negative indices for accessing elements from the end of the array
- Option to invert the selection to get all values except those at specified indices
Inputs
The input array from which to retrieve values.
The index or array of indices to retrieve. Can be a single number or an array of numbers.
Outputs
The value(s) retrieved from the input array at the specified index/indices. If a single index is provided or only one value is selected, this will be a single value. If multiple indices are provided, this will be an array of values. If no values are selected, this output will be control-flow-excluded.
Editor Settings
When enabled, inverts the selection to return all values except those at the specified indices.
Example: Extracting Specific Elements from an Array
- Create an Array Block with values
[10, 20, 30, 40, 50]
. - Add an Index Block and connect the Array Block to its
array
input. - Create a Number Block with value
2
(or an Array Block with values[0, 2, 4]
) and connect it to the Index Block’sindicies
input. - Run the flow. The Index Block’s
output
will be30
(or[10, 30, 50]
if using an array of indices).
Error Handling
- If an index is out of range (less than 0 or greater than array length), the block will throw an error
- If the array input is not an array, the block will throw an error
- If no values are selected, the output will be control-flow-excluded
FAQ
Can I use negative indices?
Can I use negative indices?
Yes, negative indices are supported. They count from the end of the array, so -1 refers to the last element, -2 to the second-to-last, and so on.
What happens if I provide duplicate indices?
What happens if I provide duplicate indices?
If you provide an array of indices with duplicates, the block will return the corresponding values, including duplicates. For example, with input array [10, 20, 30]
and indices [0, 1, 1]
, the output will be [10, 20, 20]
.
How does the invert option work?
How does the invert option work?
When invert is enabled, the block will return all values from the array except those at the specified indices. For example, with input array [10, 20, 30, 40, 50]
and indices [1, 3]
, the output with invert enabled would be [10, 30, 50]
.