Skip to content

takeItemsRight — GTM Variable Template for Array

VARIABLES › ARRAY
takeItemsRight EXTENDED Array

Returns the last N elements from an array.



Examples

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

GTM Configuration

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


Under the Hood

📜 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 ? data.cnt : count;
   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