Contact Us 1-800-596-4880

indexOf

indexOf(array: Array, value: Any): Number

Returns the index of the first occurrence of the specified element in this array, or -1 if this list does not contain the element.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

array

The array of elements to search.

value

The value to search.

Example

This example shows how indexOf behaves given different inputs.

Source

%dw 2.0
output application/json
---
{
  present: ["a","b","c","d"] indexOf "c",
  notPresent: ["x","w","x"] indexOf "c",
  presentMoreThanOnce: ["a","b","c","c"] indexOf "c",
}

Output

{
   "present": 2,
   "notPresent": -1,
   "presentMoreThanOnce": 2
 }

indexOf(theString: String, search: String): Number

Returns the index of the first occurrence of the specified String in this String.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

theString

The string to search.

search

The string to find within theString.

Example

This example shows how the indexOf behaves under different inputs.

Source

%dw 2.0
output application/json
---
{
  present: "abcd" indexOf "c",
  notPresent: "xyz" indexOf "c",
  presentMoreThanOnce: "abcdc" indexOf "c",
}

Output

{
   "present": 2,
   "notPresent": -1,
   "presentMoreThanOnce": 2
 }

indexOf(array: Null, value: Any): Number

Helper method to make indexOf null friendly

Introduced in DataWeave version 2.4.0.