lastIndexOf — GTM Variable Template for Array
lastIndexOf EXTENDED Array
Returns the index of the last occurrence of a specified value in a string or array. Returns -1 if not found.
Examples
Last index in array
INPUT
Input (String or Array): [1, 2, 3, 2, 5]
Search Value: 2
Search Value: 2
OUTPUT
3
Not found returns -1
INPUT
Input (String or Array): hello world
Search Value: z
Search Value: z
OUTPUT
-1
GTM Configuration
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]
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
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
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
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
lastIndexOf()
Related Variables
Same category: Array
Under the Hood
📜 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 ? data.val : searchElement;
fromIndex = data.rp2 ? data.idx : fromIndex;
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