Skip to content

findIndexByCallback — GTM Variable Template for Array

VARIABLES › ARRAY
findIndexByCallback EXTENDED Array

Returns the index of the first element that satisfies the given callback function. Returns -1 if not found.



Examples

Find element index
INPUT
Input Array: [1, 2, 3, 4, 5]
Test Function: function(x) { return x > 3; }
Start Index: 0
OUTPUT
3
No match returns -1
INPUT
Input Array: [1, 2, 3, 4, 5]
Test Function: function(x) { return x > 10; }
Start Index: 0
OUTPUT
-1

GTM Configuration

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

findIndexByCallback
Input Array
💾 An array or comma-separated string of values.

Supported formats:
  ✓ Array
  ✓ String
Test Function
💾 A callback function to test each item. Should return a boolean.

Supported formats:
  ✓ Function
Start Index
💾 Optional index to start the search from (default: 0).

Supported formats:
  ✓ Number
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.
Input Array array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Test Function function
Start Index number
findIndexByCallback()


Under the Hood

📜 View Implementation Code
/**
* Finds the first index in an array for which a provided function returns true.
* 
* @param {Array|string} data.src - An array or comma-separated string of values.
* @param {Function} data.fnc - A callback function to test each item. Should return a boolean.
* @param {number} data.idx - Optional index to start the search from (default: 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 matching item, or -1 if no match is found.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');

const findIndexByCallback = function(array, matchFn, fromIndex) {
   const arr = typeof array === 'string' ? array.split(',') : array;
   const startIndex = typeof fromIndex === 'number' ? fromIndex : 0;
   
   if (getType(arr) !== 'array' || typeof matchFn !== 'function') { 
       return -1; 
   }
   
   for (var i = startIndex; i < arr.length; i++) {
       var item = arr[i];
       if (matchFn(item, i, arr)) {
           return i;
       }
   }
   return -1;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// findIndexByCallback - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedArray = applyCast(data.pre, data.src);
return out(findIndexByCallback(processedArray, data.fnc, data.idx));
// ===============================================================================
// findIndexByCallback(...) – Apply Mode
// ===============================================================================
/*
return function(value, matchFn, fromIndex) {
   matchFn = data.rp1 ? data.fnc : matchFn;
   return out(findIndexByCallback(value, matchFn, data.idx));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Find element index'
✅ Comma-separated string - splits and finds match
✅ Array with fromIndex - starts search from specified index
✅ '[example] No match returns -1'
✅ Non-array input - returns -1