Array

Arrays can be declared as constants in a flowgraph or variables in user defined functions. To create an array from your data in a Juttle pipeline, use the pluck reducer.

The Array module exposes a limited set of operations on array variables.

Array.concat

Combine the elements of a list of arrays into a single array

Array.concat(array1, array2, ...)
Parameter Description Required?
array1 The first elements in the resulting array Yes
array2 Elements to append to array1 No
... Any number of additional arrays No

Example: Concatenate ["hello"], [] and ["world"] into ["hello", "world"]

emit -points [{hello: ['hello'], world: ['world']}]
| put message = Array.concat(hello, [], world)
| keep message

Array.indexOf

Search an array and return the zero-based index of the first match, or -1 if there is no match.

Array.indexOf(array, searchvalue)
Parameter Description Required?
array The array to search Yes
searchvalue The value for which to search Yes

Example: Search for a random integer in an array of random integers

emit -limit 10 -every :.1 second:
| put values=(Math.round(Math.random()*10))
| reduce pluck(values)
| put searchvalue=(Math.round(Math.random()*10))
| put index=Array.indexOf(pluck, searchvalue)

Array.join

Combine the elements of an array into a string

Array.join(array, separator)
Parameter Description Required?
array Array whose elements to join Yes
separator Separator to delimit the joined elements No; defaults to ','

Example: Join ['joe','meg','bob','may'] into the string 'joe,meg,bob,may'

emit -points [{people: Array.join(['joe','meg','bob','may'], ',')}]

Array.lastIndexOf

Search an array and return the zero-based index of the last match, or -1 if there is no match.

Array.lastIndexOf(array, searchvalue)
Parameter Description Required?
array The array to search Yes
searchvalue The value for which to search Yes

Example: Search for a random integer in an array of random integers

emit -points [{result: Array.lastIndexOf(['A','B', 'B'], 'B')}]

Array.length

Return the number of items in an array.

Array.length(array)
Parameter Description Required?
array The array to measure Yes

Example: Create an array containing a random number of values, then count them

const random_length=(Math.round(Math.random()*10));
;
emit -limit random_length -every :.1 second:
| put values=(Math.round(Math.random()*10))
| reduce pluck(values)
| put length=Array.length(pluck)

Array.pop

Remove the last element of an array and return it

Array.pop(array)
Parameter Description Required?
array Array to pop an element from Yes

Example: Pop the element 'b' off ['a', 'b']

emit -points [{array: ['a','b']}]
| put result = Array.pop(array)

Array.push

Add an element to the end of an array

Array.push(array, element)
Parameter Description Required?
array Array to add elementto Yes
element Element to add Yes

Example: Push 'pajama' and 'potatoes' into the array ['banana']

emit -points [{array: ["banana"]}]
| put elements = Array.push(array, 'pajama', 'potatoes')

Array.reverse

Reverse the order of elements in an array

Array.reverse(array)
Parameter Description Required?
array Array reverse Yes

Example: Reverse ['a', 'b'], yielding ['b', 'a']

emit -points [{array: ['a','b']}]
| put result = Array.reverse(array)

Array.shift

Removes and returns the first element of an array

Array.shift(array)
Parameter Description Required?
array Array to take first element from Yes

Example: Take the element 'a' out of ['a', 'b']

emit -points [{array: ['a','b']}] 
| put result = Array.shift(array)

Array.slice

Returns a subarray of the given array

Array.slice(array, start, end)
Parameter Description Required?
array Array to return a slice of Yes
start Index of subarray start No; defaults to 0
end Index of subarray end No; defaults to end of array

Example: Take the slice ['banana'] of the array ['banana', 'pajama']

emit -points [{array: ["banana", "pajama"]}]
| put elements = Array.slice(array, 0, 1)
| keep elements, array

Array.sort

Sort an array. If the array contains numbers, they are sorted in increasing order. If it contains strings, they are sorted alphabetically. Sorting a mixed array is an error.

Array.sort(array)
Parameter Description Required?
array Array to sort Yes

Example: Sort ['c', 'a', 'b'], yielding ['a', 'b', 'c'].

emit -points [{array: ['c', 'a','b']}]
| put result = Array.sort(array)

Array.splice

Removes and returns a subarray, and replaces the subarray with given elements in the input array

Array.splice(array, start, end, elements...)
Parameter Description Required?
array Array to splice Yes
start Start of subarray to replace No; defaults to 0
end End of subarray No; defaults to end of array
elements List of elements to add No; defaults to none

Example: Replace 'banana' with 'potatoes' and return ['banana'] given the array ['banana', 'pajama'].

emit -points [{array: ["banana", "pajama"]}]
| put elements = Array.splice(array, 0, 1, 'potatoes')

Array.toString

Returns a string representation of a given array.

Array.toString(array)
Parameter Description Required?
array The array to stringify Yes

Example: Stringify ["hello", "goodbye"], yielding the string ["hello", "goodbye"].

emit -points [{result: Array.toString(["hello", "goodbye"])}]

Array.unshift

Add an element to the beginning of an array

Array.unshift(array, element)
Parameter Description Required?
array Array to add element to Yes
element Element to add Yes

Example: Add 'pajama' and 'potatoes' to the beginning of ['banana']

emit -points [{array: ["banana"]}]
| put elements = Array.unshift(array, 'pajama', 'potatoes')