arrayLastIndexOf

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. The returned index value is always relative to the beginning of the array — the first element is at index 0, the second at 1, and so on. The original array is not modified.

Potential use case

Use this task to find the last occurrence of a particular element in an array. For example, to find the last time a particular email appeared in an email list, use arrayLastIndexOf to retrieve its most recent index. If the email is not present, the result is -1.

Properties

IncomingTypeDescription
arrArrayRequired. The array to search.
searchElementAnyRequired. The element to locate.
fromIndexNumberOptional. The index position from which to start searching backwards. A positive number counts from the beginning (index 0). A negative number counts from the end. If not provided, the entire array is searched.
OutgoingTypeDescription
indexNumberThe last index where the element is found; -1 if not present.

Example 1

In this example:

  • The arr is statically set to ["a","b","c","a","b","c"].
  • The searchElement is "b".
  • fromIndex is not provided, so the entire array is searched.
  • The result is index 4 — the last position of "b" in the array.
arrayLastIndexOf task returning index 4 for the last occurrence of b

Example 2

In this example:

  • The arr is [1,2,3,2,5,2,3,2].
  • The searchElement is 2.
  • The fromIndex is -2, which means the search starts two positions from the end.
  • The result is index 5 — the last position of 2 within the search range.
arrayLastIndexOf task with fromIndex -2 returning index 5 for the last occurrence of 2