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.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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
Live Sandbox
Section titled βLive Sandboxβ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
Section titled βRelated VariablesβSame category: Array
Under the Hood
Section titled β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 ? 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