Contact Us 1-800-596-4880

lastIndexOf

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

Returns the index of the last occurrence of the specified element in a given array or -1 if the array 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"] lastIndexOf "c",
  notPresent: ["x","w","x"] lastIndexOf "c",
  presentMoreThanOnce: ["a","b","c","c"] lastIndexOf "c",
}

Output

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

lastIndexOf(array: String, value: String): Number

Takes a string as input and returns the index of the last occurrence of a given search string within the input. The function returns -1 if the search string is not present in the input.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

string

The string to search.

value

A string value to search for within the input string.

Example

This example shows how the indexOf behaves given different inputs.

Source

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

Output

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

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

Helper function that enables lastIndexOf to work with a null value.

Introduced in DataWeave version 2.4.0.