Skip to content

selectFromArray — GTM Variable Template for Array

VARIABLES › ARRAY
selectFromArray CORE Array

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


When to Use This

Array Processing

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


Examples

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

GTM Configuration

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


Under the Hood

📜 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 ? data.idx : index;
   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