Task References

On this page:

lastIndexOf (array)


The arrayLastIndexOf task searches an array and returns the last index (position) at which a given element is found. The task returns -1 if the element is not present. The array is searched backwards, beginning at the fromIndex position. Lastly, this task does not change the array.

Although the task searches backwards (i.e., from back to front), it returns a location value (index number) that is relative to the beginning of the array. The index of the first element at the beginning of an array is always zero (0). The index of the second element is 1, the index of the third element is 2, and so on.

Potential Use Case

This task method is used to search (find) the last position of a particular element in a given array. Suppose you have an email list and are looking to see when the last time a message was received from a particular email on that list. Using this method, you could pull up that email by the last time it had been indexed, no matter how many times it is listed within the index itself. If the email was not present in the index, the task result would be -1.


Input and output parameters are listed below.

Incoming Type Description
arr Array Required. The array to search.
searchElement Any Required. The element to locate in the array.
fromIndex Number Optional. The index position at which to start searching backwards. A positive number counts from the beginning at index "0". A negative number counts from the end. If this parameter is not provided, the whole array will be searched.

Outgoing Type Description
index Number The last index where the element within the array is found; -1 if not present.


Example 1

In the IAP example below:

  • The arr variable is statically set with ["a","b","c","a","b","c"] in the reference variable.

  • The searchElement to find is b.

  • The fromIndex is not provided. The default will be to search the whole array.

  • The automation will return an index of 4. This means the index for the letter "b" is located at position "4" in the array.


Example 2

In the IAP example below:

  • The arr reference variable is [1,2,3,2,5,2,3,2].

  • The element to search for is the number 2.

  • The fromIndex position to start the search is -2, which means the index counter is from the end.

  • The automation will return an index of 5, which is the last index position of the searchElement that we are looking for, and it is located three places from the right on the index.