Skip to content

takeItemsRight β€” GTM Variable Template for Array

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

Returns the last N elements from an array.



Take last 3
INPUT
Array To Process: [1, 2, 3, 4, 5, 6]
Count: 3
OUTPUT
[4, 5, 6]
Count exceeds length
INPUT
Array To Process: ['a', 'b', 'c']
Count: 10
OUTPUT
['a', 'b', 'c']

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

takeItemsRight
Array To Process
πŸ’Ύ The array to take items from the end.

Supported formats:
  βœ“ Array
Count
πŸ’Ύ The number of items to take from the end.

Supported formats:
  βœ“ Number
  βœ“ String
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 Process array
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Count number
takeItemsRight()


πŸ“œ View Implementation Code
/**
* Returns the last count elements from the array.
*
* @param {Array} data.src - The array to take items from.
* @param {number|string} data.cnt - The number of items to take from the end.
* @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 taking items.
*
* @returns {Array} A new array with up to count elements from the end, or empty array if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const Math = require('Math');
const makeNumber = require('makeNumber');
const takeItemsRight = function(arr, count) {
const cnt = makeNumber(count);
if (getType(arr) !== 'array' || cnt !== cnt || cnt < 0) {
return [];
}
return arr.slice(Math.max(arr.length - cnt, 0));
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// takeItemsRight - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(takeItemsRight(value, data.cnt));
// ===============================================================================
// takeItemsRight(...) – Apply Mode
// ===============================================================================
/*
return function(value, count) {
count = data.rp1 ? count : data.cnt;
return out(takeItemsRight(value, count));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Take last 3'
βœ… '[example] Count exceeds length'
βœ… String count parameter - converts to number and takes items
βœ… Negative count - returns empty array
βœ… Non-array input - returns empty array