compact — GTM Variable Template for Array
compact EXTENDED Array
Removes all falsy values from an array (false, null, 0, "", undefined, NaN).
Examples
Remove falsy values
INPUT
Array To Clean: [1, false, 2, null, 3, 0, 4, '', 5, undefined]
OUTPUT
[1, 2, 3, 4, 5]
No falsy values unchanged
INPUT
Array To Clean: [1, 'hello', true, {}, []]
OUTPUT
[1, 'hello', true, {}, []]
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
compact
Array To Clean
💾 The array to clean of falsy values.
Supported formats:
✓ Array
Supported formats:
✓ Array
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.
Array To Clean array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
compact()
Related Variables
Same category: Array
Under the Hood
📜 View Implementation Code
/**
* Removes all falsy values from the input array.
*
* @param {Array} data.src - The array to clean of falsy values.
* @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 cleaning.
*
* @returns {Array} A new array with only truthy values.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const compact = function(arr) {
if (getType(arr) !== 'array') {
return [];
}
const result = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
result.push(arr[i]);
}
}
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// compact - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(compact(value));
// ===============================================================================
// compact() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(compact(value));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Remove falsy values'
✅ '[example] No falsy values unchanged'
✅ Array with only falsy values - should return empty array
✅ Empty array - should return empty array
✅ Non-array input - should return empty array