Skip to content

filterArray β€” GTM Variable Template for Array

VARIABLES β€Ί ARRAY
filterArray EXTENDED Array
Direct (.tpl) Apply (.tpl)

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



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
[]

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()


πŸ“œ 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 ? callback : data.cbk;
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