%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",
}
DataWeave
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 |
---|---|
|
The array of elements to search. |
|
The value to search. |
Example
This example shows how indexOf
behaves given different inputs.
Source
Output
{
"present": 2,
"notPresent": -1,
"presentMoreThanOnce": 2
}
Json
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 |
---|---|
|
The string to search. |
|
The string to find within |
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",
}
DataWeave
Output
{
"present": 2,
"notPresent": -1,
"presentMoreThanOnce": 2
}
Json
indexOf(array: Null, value: Any): Number
Helper method to make indexOf null friendly
Introduced in DataWeave version 2.4.0.