indexOf (array)
  • 23 May 2023
  • Dark
    Light
  • PDF

indexOf (array)

  • Dark
    Light
  • PDF

Article summary

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 is Banana.

  • The fromIndex variable will default to 0.

  • The index result will return 2 upon output, indicating that the searchElement ("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.

    indexOfArray


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.