Skip to content

lastIndexOf β€” GTM Variable Template for Array

VARIABLES β€Ί ARRAY
lastIndexOf EXTENDED Array
Direct (.tpl) Apply (.tpl)

Returns the index of the last occurrence of a specified value in a string or array. Returns -1 if not found.



Last index in array
INPUT
Input (String or Array): [1, 2, 3, 2, 5]
Search Value: 2
OUTPUT
3
Not found returns -1
INPUT
Input (String or Array): hello world
Search Value: z
OUTPUT
-1

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

lastIndexOf
Input (String or Array)
πŸ’Ύ The string or array to search for the last occurrence.

Supported formats:
  βœ“ String: "hello world"
  βœ“ Array: [1, 2, 3, 2, 1]
Search Value
πŸ’Ύ The value to search for. For strings, searches for substring. For arrays, searches for element.

Supported formats:
  βœ“ Any type
From Index (optional)
πŸ’Ύ Optional position to start searching backwards from. If not provided, searches from the end. Negative values return -1.

Supported formats:
  βœ“ Number: 5, 10
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before searching (e.g., normalize case, filter array).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the index result before returning it (e.g., idx => idx !== -1 for boolean, idx => idx + 1 for 1-based indexing). Useful for chaining transformations on the output.
Input (String or Array) array
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Search Value number
From Index (optional) number
lastIndexOf()


πŸ“œ View Implementation Code
/**
* Returns the index of the last occurrence of a specified value in a string or an array.
*
* @param {string|Array} data.src - The string or array to search.
* @param {any} data.val - The element to search for in the string or array.
* @param {number} [data.idx] - Optional position at which to start the search (searches backwards from this index).
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before searching.
*
* @returns {number} The index of the last occurrence of the value, or -1 if not found or input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const lastIndexOf = function(input, searchElement, fromIndex) {
const inputType = getType(input);
if (inputType !== 'array' && inputType !== 'string') {
return -1;
}
// Use default fromIndex if not provided
const startIndex = typeof fromIndex === 'number' ? fromIndex : input.length - 1;
if (startIndex < 0) {
return -1;
}
return input.lastIndexOf(searchElement, startIndex);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// lastIndexOf - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(lastIndexOf(value, data.val, data.idx));
// ===============================================================================
// lastIndexOf(...) – Apply Mode
// ===============================================================================
/*
return function(input, searchElement, fromIndex) {
searchElement = data.rp1 ? searchElement : data.val;
fromIndex = data.rp2 ? fromIndex : data.idx;
return out(lastIndexOf(input, searchElement, fromIndex));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… Find last occurrence of character in string
βœ… '[example] Last index in array'
βœ… Search with fromIndex parameter limiting search range
βœ… '[example] Not found returns -1'
βœ… Invalid input type returns -1