Skip to content

selectFromArray β€” GTM Variable Template for Array

VARIABLES β€Ί ARRAY
selectFromArray CORE Array
Direct (.tpl) Apply (.tpl)

Selects an element from the array based on the provided index. Returns undefined if invalid.


Array Processing

Iterate, filter, map, and reshape arrays of items for batch data operations.


Select by index
INPUT
Array To Select From: ['apple', 'banana', 'cherry', 'date']
Index: 2
OUTPUT
cherry
Out of bounds returns undefined
INPUT
Array To Select From: [1, 2, 3]
Index: 10
OUTPUT
undefined

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

selectFromArray
Array To Select From
πŸ’Ύ The array from which to select an element.

Supported formats:
  βœ“ Array
Index
🎯 The index of the element to select.

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 Select From array
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Index number
selectFromArray()


πŸ“œ View Implementation Code
/**
* Selects an element from the array based on the provided index.
*
* @param {Array} data.src - The array from which to select an element.
* @param {number|string} data.idx - The index of the element to select.
* @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 selection.
*
* @returns {any} The selected element at the specified index, or undefined if the index is invalid or array is not valid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require("getType");
const makeNumber = require('makeNumber');
const selectFromArray = function(array, index) {
if (getType(array) === 'array') {
const idx = makeNumber(index);
return array[idx];
}
return undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// selectFromArray - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(selectFromArray(value, data.idx));
// ===============================================================================
// selectFromArray(...) – Apply Mode
// ===============================================================================
/*
return function(value, index) {
index = data.rp1 ? index : data.idx;
return out(selectFromArray(value, index));
};
*/
πŸ§ͺ View Test Scenarios (6 tests)
βœ… '[example] Select by index'
βœ… String index parameter - converts to number and selects element
βœ… Index zero - returns first element
βœ… '[example] Out of bounds returns undefined'
βœ… Non-array input - returns undefined
βœ… Non-integer index - returns undefined