chooseFromArray
The chooseFromArray
transformer is used to choose one or multiple elements from an array.
If a non-array value is passed to this transformer, the value is returned as-is.
Returns null
if the input array is empty.
The chooseFromArray
transformer has the following properties:
indices
: A list of indices to choose from. Returns the inner object if only one index is specified. Returns an error if an index does not exist. If not specified, a random element will be returned.
Examples
Choose one random element from an array
{
"type": "array",
"values": [1, 2, 3, 4, 5],
"transform": [
{
"type": "chooseFromArray"
}
]
}
Choose the second element from an array
{
"type": "array",
"values": [1, 2, 3, 4, 5],
"transform": [
{
"type": "chooseFromArray",
"indices": [1]
}
]
}
Choose multiple elements from an array
{
"type": "array",
"values": [1, 2, 3, 4, 5],
"transform": [
{
"type": "chooseFromArray",
"indices": [1, 3]
}
]
}