Sometimes your scenario may have a deal with Javascript arrays. For example, if your bot needs to Read data from Google Sheets. Thus you may need some tools to iterate over the arrays in scenarios. pick random item and etc.

This article describes the ways you can manipulate the data in the arrays in your scenarios.

Underscore

Aimylogic has a built-in integration with underscore library that contains a set of special functions for arrays and collections. You can use these functions to transform arrays on the fly and obtain the data you need.

Additional functions

Aimylogic also provides a set of additional functions that enable you to iterate over arrays and pick a random item.

Each function returns some result. Fro example, if you have an array named items you can easily fetch the next item this way

$items.next()

Or if you need to pick a random one just call

$items.random()

Here is a full list of supported functions.

first
This function returns the first element from an array or undefined if an array is empty. Also this function resets the internal iterator, so if you'd like to iterate over the same array from scratch on the next user's request, just invoke this function firstly.

next
This function returns the next element or undefined if the end of array was achieved.

prev
Returns the previous element or undefined if an array is empty or the internal iterator points to the very first element.

random
Returns random element from an array or undefined if an array is empty. Also moves the internal iterator to the selected element.

current
Returns the current element or undefined if an internal iterator didn't move previously by any functions above.
Was this article helpful?
Thank you!