reduce β GTM Variable Template for Array
reduce EXTENDED Array
Reduces an array to a single value using a callback function and an optional initial value.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβSum numbers
INPUT
Array To Reduce: [1, 2, 3, 4, 5]
Reducer Function: function(acc, val) { return acc + val; }
Initial Value: 0
Use Initial Value: true
Reducer Function: function(acc, val) { return acc + val; }
Initial Value: 0
Use Initial Value: true
OUTPUT
15
Concatenate strings
INPUT
Array To Reduce: ['hello', ' ', 'world']
Reducer Function: function(acc, val) { return acc + val; }
Initial Value:
Use Initial Value: true
Reducer Function: function(acc, val) { return acc + val; }
Initial Value:
Use Initial Value: true
OUTPUT
hello world
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
reduce
Array To Reduce
πΎ The array to reduce to a single value.
Supported formats:
β Array
Supported formats:
β Array
Reducer Function
πΎ A reducer function of the form (accumulator, currentValue) => nextAccumulator.
Supported formats:
β Function
Supported formats:
β Function
Initial Value
πΎ An optional initial value for the accumulator.
Supported formats:
β Any
Supported formats:
β Any
Use Initial Value
πΎ Whether to use the initial value.
Options:
β Yes/No
Options:
β Yes/No
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the array before internal logic (e.g., filter elements, sort array). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., val => Math.round(val), val => val.toString() for string conversion). Useful for chaining transformations on the output.
Array To Reduce array
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Reducer Function function
Initial Value any
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
reduce()
Related Variables
Section titled βRelated VariablesβSame category: Array
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Reduces an array to a single value using a callback function and an optional initial value. * * @param {Array} data.arr - The array to reduce. * @param {Function} data.fnc - A reducer function of the form (accumulator, currentValue) => nextAccumulator. * @param {*} data.ini - An optional initial value for the accumulator. * @param {boolean} data.use - Whether to use the initial value. * @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 arr before reducing. * * @returns {*|undefined} The final accumulated result from the reduce operation. Returns undefined if the input is not a valid array or the callback is not a function. * * @framework ggLowCodeGTMKit */const getType = require('getType');const reduce = function(array, callback, initialValue, useInitial) { if (getType(array) !== 'array' || typeof callback !== 'function') { return undefined; } return useInitial ? array.reduce(callback, initialValue) : array.reduce(callback);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// reduce - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedArray = applyCast(data.pre, data.arr);return out(reduce(processedArray, data.fnc, data.ini, data.use));// ===============================================================================// reduce(...) β Apply Mode// ===============================================================================/*return function(array, callback, initialValue, useInitial) { callback = data.rp1 ? callback : data.fnc; initialValue = data.rp2 ? initialValue : data.ini; useInitial = data.rp3 ? useInitial : data.use; return out(reduce(array, callback, initialValue, useInitial));};*/π§ͺ View Test Scenarios (12 tests)
β
'[example] Sum numbers'β
Sum of numbers without initial value - should return sumβ
Product of numbers with initial value - should return productβ
'[example] Concatenate strings'β
Build object from array with initial value - should return objectβ
Flatten nested arrays with initial value - should return flat arrayβ
Single element array with initial value - should return combined resultβ
Single element array without initial value - should return the elementβ
Empty array with initial value - should return initial valueβ
Non-array input - should return undefinedβ
Non-function callback - should return undefinedβ
Find maximum value with initial value - should return max