Skip to content

reduceRight β€” GTM Variable Template for Array

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

Reduces an array from right to left using a callback function and an optional initial value.



Reverse concatenation
INPUT
Array To Reduce: ['a', 'b', 'c', 'd']
Reducer Function: function(acc, val) { return acc + val; }
Initial Value:
Use Initial Value: true
OUTPUT
dcba
Sum from right
INPUT
Array To Reduce: [1, 2, 3, 4, 5]
Reducer Function: function(acc, val) { return acc + val; }
Initial Value: undefined
Use Initial Value: false
OUTPUT
15

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

reduceRight
Array To Reduce
πŸ’Ύ The array to reduce from right to left.

Supported formats:
  βœ“ Array
Reducer Function
πŸ’Ύ A reducer function of the form (accumulator, currentValue) => nextAccumulator.

Supported formats:
  βœ“ Function
Initial Value
πŸ’Ύ An optional initial value for the accumulator.

Supported formats:
  βœ“ Any
Use Initial Value
πŸ’Ύ Whether to use the initial value.

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


πŸ“œ View Implementation Code
/**
* Reduces an array from right to left 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 right-to-left 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 reduceRight = function(array, callback, initialValue, useInitial) {
if (getType(array) !== 'array' || typeof callback !== 'function') {
return undefined;
}
return useInitial ? array.reduceRight(callback, initialValue) : array.reduceRight(callback);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// reduceRight - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedArray = applyCast(data.pre, data.arr);
return out(reduceRight(processedArray, data.fnc, data.ini, data.use));
// ===============================================================================
// reduceRight(...) – Apply Mode
// ===============================================================================
/*
return function(array, callback, initialValue, useInitial) {
callback = data.rp1 ? callback : data.fnc;
return out(reduceRight(array, callback, data.ini, data.use));
};
*/
πŸ§ͺ View Test Scenarios (11 tests)
βœ… '[example] Reverse concatenation'
βœ… Subtract numbers from right to left with initial value
βœ… Build array from right to left with initial value
βœ… Flatten nested arrays from right to left with initial value
βœ… '[example] Sum from right'
βœ… 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-function callback - should return undefined
βœ… Compose functions from right to left
βœ… Build nested structure from right to left with initial value