Skip to content

find — GTM Variable Template for Array

VARIABLES › ARRAY
find EXTENDED Array

Returns the first element in an array that satisfies the given predicate function.



Examples

Find first match
INPUT
Input Array: [1, 3, 4, 5, 6]
Test Function: function(x) { return x % 2 === 0; }
OUTPUT
4
No match returns undefined
INPUT
Input Array: [1, 3, 5, 7]
Test Function: function(x) { return x % 2 === 0; }
OUTPUT
undefined

GTM Configuration

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

find
Input Array
💾 The array to search in.

Supported formats:
  ✓ Array
Test Function
💾 A function to test each element. Should return true for the desired element.

Supported formats:
  ✓ Function
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
find()


Under the Hood

📜 View Implementation Code
/**
* Finds the first element in an array that satisfies a provided testing function.
* 
* @param {Array} data.src - The array to search in.
* @param {Function} data.fnc - A function to test each element. Should return true for the desired element.
* @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 {*} The first element that satisfies the testing function, or undefined if no element is found.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const find = function(arr, testFunction) {
   if (getType(arr) !== 'array' || typeof testFunction !== 'function') {
       return undefined;
   }
   
   for (let i = 0; i < arr.length; i++) {
       if (testFunction(arr[i], i, arr)) {
           return arr[i];
       }
   }
   return undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// find - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedArray = applyCast(data.pre, data.src);
return out(find(processedArray, data.fnc));
// ===============================================================================
// find(...) – Apply Mode
// ===============================================================================
/*
return function(value, testFunction) {
   testFunction = data.rp1 ? data.fnc : testFunction;
   return out(find(value, testFunction));
};
*/
🧪 View Test Scenarios (6 tests)
✅ '[example] Find first match'
✅ Find first string longer than 3 characters
✅ '[example] No match returns undefined'
✅ Find first element (returns first match)
✅ Empty array returns undefined
✅ Find object with specific property