indexOf β GTM Variable Template for Array
indexOf EXTENDED Array
Returns the index of the first occurrence of a value in a string or array.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβFind index in string
INPUT
String or Array: hello world
Search Element: o
βΆοΈ From Index: 0
Search Element: o
βΆοΈ From Index: 0
OUTPUT
4
Not found returns -1
INPUT
String or Array: hello world
Search Element: z
βΆοΈ From Index: 0
Search Element: z
βΆοΈ From Index: 0
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.
indexOf
String or Array
πΎ The string or array to search in.
Supported formats:
β String
β Array
Supported formats:
β String
β Array
Search Element
πΎ The element to search for in the string or array.
Supported formats:
β Any
Supported formats:
β Any
Advanced Setting
βΆοΈ From Index
πΎ The position at which to start the search (default is 0).
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
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 Element any
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
indexOf()
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 first 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.elm - The element to search for in the string or array.* @param {number|string} data.frm - The position at which to start the search (default is 0).* @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 first occurrence of the search element, or -1 if not found.** @framework ggLowCodeGTMKit*/const getType = require('getType');const makeNumber = require('makeNumber');const indexOf = function(input, searchElement, fromIndex) { const fromIdx = fromIndex !== undefined ? makeNumber(fromIndex) : 0; if (!(getType(input) === 'array' || getType(input) === 'string') || typeof fromIdx !== 'number' || fromIdx < 0) { return -1; } return input.indexOf(searchElement, fromIdx);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// indexOf - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(indexOf(value, data.elm, data.frm));// ===============================================================================// indexOf(...) β Apply Mode// ===============================================================================/*return function(value, searchElement, fromIndex) { searchElement = data.rp1 ? data.elm : searchElement; fromIndex = data.rp2 ? data.frm : fromIndex; return out(indexOf(value, searchElement, fromIndex));};*/π§ͺ View Test Scenarios (9 tests)
β
'[example] Find index in string'β
'[example] Not found returns -1'β
String with fromIndex specified - should search from that positionβ
Array with matching element - should return indexβ
Array with element not found - should return -1β
Array with fromIndex specified - should search from that positionβ
String with fromIndex as string - should convert and use itβ
Array searching from middle position - should skip earlier occurrencesβ
fromIndex beyond array length - should return -1