Skip to content

dropItemsRight — GTM Variable Template for Array

VARIABLES › ARRAY
dropItemsRight EXTENDED Array

Removes the last N elements from an array and returns the rest.



Examples

Drop last 2 items
INPUT
Array To Process: [1, 2, 3, 4, 5]
Count: 2
OUTPUT
[1, 2, 3]
Drop all returns empty
INPUT
Array To Process: [1, 2, 3]
Count: 10
OUTPUT
[]

GTM Configuration

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

dropItemsRight
Array To Process
💾 The array to remove items from the end.

Supported formats:
  ✓ Array
Count
💾 The number of items to remove 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
dropItemsRight()


Under the Hood

📜 View Implementation Code
/**
 * Removes the last count elements from the array and returns the rest.
 * 
 * @param {Array} data.src - The array to trim.
 * @param {number|string} data.cnt - The number of items to remove 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 dropping items.
 * 
 * @returns {Array} A new array with the last count items removed, or empty array if input is invalid.
 *
 * @framework ggLowCodeGTMKit
 */
const getType = require('getType');
const Math = require('Math');
const makeNumber = require('makeNumber');

const dropItemsRight = function(arr, count) {
    const cnt = makeNumber(count);
    if (getType(arr) !== 'array' || cnt !== cnt || cnt < 0) {
        return [];
    }
    return arr.slice(0, Math.max(0, arr.length - cnt));
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// dropItemsRight - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(dropItemsRight(value, data.cnt));
// ===============================================================================
// dropItemsRight(...) – Apply Mode
// ===============================================================================
/*
return function(value, count) {
   count = data.rp1 ? data.cnt : count;
   return out(dropItemsRight(value, count));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Drop last 2 items'
✅ String count parameter - converts to number and drops elements
✅ '[example] Drop all returns empty'
✅ Negative count - returns empty array
✅ Non-array input - returns empty array