dropItems — GTM Variable Template for Array
dropItems EXTENDED Array
Removes the first N elements from an array and returns the rest.
Examples
Drop first 2 items
INPUT
Array To Process: [1, 2, 3, 4, 5]
Count: 2
Count: 2
OUTPUT
[3, 4, 5]
Drop all returns empty
INPUT
Array To Process: [1, 2, 3]
Count: 10
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.
dropItems
Array To Process
💾 The array to drop items from.
Supported formats:
✓ Array
Supported formats:
✓ Array
Count
🎯 The number of items to drop from the beginning.
Supported formats:
✓ Number
✓ String
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
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
dropItems()
Related Variables
Same category: Array
Under the Hood
📜 View Implementation Code
/**
* Removes the first count elements from the array and returns the rest.
*
* @param {Array} data.src - The array to drop items from.
* @param {number|string} data.cnt - The number of items to drop from the beginning.
* @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 first count items removed, or empty array if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');
const dropItems = function(arr, count) {
const cnt = makeNumber(count);
if (getType(arr) !== 'array' || cnt !== cnt || cnt < 0) {
return [];
}
return arr.slice(cnt);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// dropItems - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(dropItems(value, data.cnt));
// ===============================================================================
// dropItems(...) – Apply Mode
// ===============================================================================
/*
return function(value, count) {
count = data.rp1 ? data.cnt : count;
return out(dropItems(value, count));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Drop first 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