Skip to content

filterArray — GTM Variable Template for Array

VARIABLES › ARRAY
filterArray EXTENDED Array

Filters an array using a callback function, returning only elements that pass the test.



Examples

Filter even numbers
INPUT
Input Array: [1, 2, 3, 4, 5, 6]
Filter Function: function(x) { return x % 2 === 0; }
OUTPUT
[2, 4, 6]
Empty array unchanged
INPUT
Input Array: []
Filter Function: function(x) { return x > 0; }
OUTPUT
[]

GTM Configuration

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

filterArray
Input Array
💾 The array of values to filter.

Supported formats:
  ✓ Array
Filter Function
💾 A function that receives each item and returns true to keep it.

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
Filter Function function
filterArray()


Under the Hood

📜 View Implementation Code
/**
* Filters items in the array using a callback function.
* 
* @param {Array} data.src - The array of values to filter.
* @param {Function} data.cbk - A function that receives each item and returns true to keep it.
* @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 filtering.
* 
* @returns {Array} A new array containing only the items that passed the callback test.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const filterArray = function(arr, callback) {
   if (getType(arr) !== 'array' || typeof callback !== 'function') {
       return [];
   }
   return arr.filter(callback);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// filterArray - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedArray = applyCast(data.pre, data.src);
return out(filterArray(processedArray, data.cbk));
// ===============================================================================
// filterArray(...) – Apply Mode
// ===============================================================================
/*
return function(value, callback) {
   callback = data.rp1 ? data.cbk : callback;
   return out(filterArray(value, callback));
};
*/
🧪 View Test Scenarios (11 tests)
✅ '[example] Filter even numbers'
✅ Array with strings - filter strings longer than 3 characters
✅ Array with no matching items - should return empty array
✅ Array with all matching items - should return all items
✅ '[example] Empty array unchanged'
✅ Array with objects - filter by property value
✅ Array with mixed types - filter truthy values
✅ Non-array input - should return empty array
✅ Null input - should return empty array
✅ Non-function callback - should return empty array
✅ Filter with index parameter - keep items at even indices