- 23 May 2023
-
DarkLight
-
PDF
indexOf (array)
- Updated on 23 May 2023
-
DarkLight
-
PDF
indexOf (array) Task Purpose
The arrayIndexOf
task is used to return the first index (position) at which a given element can be found in the array. The task returns -1 if the element is not found.
Potential Use Case
Suppose you have a list of email addresses and want to determine if an email record is listed in the correct department. The arrayIndexOf
task can be used to check whether the email exists or not. It will return the index number (position) where the email occurs. If the email is not found in the list, then your return result would be -1.
Properties
Incoming | Type | Description |
---|---|---|
arr |
Array | Required. The array to search. |
searchElement |
Any | Required. The element to search for in the array. |
fromIndex |
Number | Optional. The index position to start the search at. Default is 0 (zero). |
Outgoing | Type | Description |
---|---|---|
index |
Number | The position of the element in the arr property. Returns -1 if not found. |
Example
In this IAP example:
-
The
arr
variable is statically specified as["Apple","Pear","Banana"]
. -
The
searchElement
reference variable isBanana
. -
The
fromIndex
variable will default to 0. -
The
index
result will return2
upon output, indicating that thesearchElement
("Banana") is located at index position 2 in the array.Note: The first element has position 0, the second element has position 1, and so on.