Skip to content

๐Ž๐๐‰๐„๐‚๐“ From Arrays Generator - Object โ€” GTM Variable Template for GTM

VARIABLES โ€บ GTM
๐Ž๐๐‰๐„๐‚๐“ From Arrays Generator - Object CORE GTM

Maps two arrays or comma-separated strings into an object. Uses the keys array length as reference โ€” missing values become undefined and extra values are ignored.


When to Use This

GTM Utilities

Access GTM-specific APIs: dataLayer, debug mode, container settings.


Examples

Pair keys and values
INPUT
keys: ['name', 'age', 'city']
vals: ['John', 30, 'Paris']
Separator (optional): undefined
Output Function (optional): undefined
OUTPUT
John
Custom separator
INPUT
keys: name|age|city
vals: John|30|Paris
Separator (optional): |
Output Function (optional): undefined
OUTPUT
John

GTM Configuration

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

๐Ž๐๐‰๐„๐‚๐“ From Arrays Generator - Object
Keys (Property Names)
๐Ÿ”‘ Array of property names or comma-separated string.

Accepted formats:
โ€ข Array: ['name', 'age', 'city']
โ€ข String: name,age,city
โ€ข Variable: {{Key Array Variable}}

Notes:
โ€ข Empty keys are skipped
โ€ข Duplicate keys: last value wins
โ€ข Special characters allowed: user.name, user-id

Examples:
โ€ข name,email,phone
โ€ข ['firstName', 'lastName', 'age']
โ€ข {{Form Field Names}}
Values
๐Ÿ’พ Array of values or comma-separated string.

Accepted formats:
โ€ข Array: ['John', 30, 'Paris']
โ€ข String: John,30,Paris
โ€ข Variable: {{Value Array Variable}}

Notes:
โ€ข Values matched to keys by position
โ€ข Missing values โ†’ undefined
โ€ข Extra values โ†’ ignored

Examples:
โ€ข John Doe,john@example.com,555-1234
โ€ข ['Alice', 'alice@test.com', '555-5678']
โ€ข {{Form Field Values}}
Options
Separator (optional)
โœ‚๏ธ Character(s) used to split strings into arrays.

Only applies when keys or vals are strings (not arrays).

Default: , (comma)

Examples:
โ€ข , โ†’ Comma (default)
โ€ข | โ†’ Pipe
โ€ข ; โ†’ Semicolon
โ€ข ; โ†’ Semicolon with space
โ€ข โ†’ Tab character

๐Ÿ’ก For arrays, this parameter is ignored.
Result Handling
Output Function (optional)
โš™๏ธ Optional function to transform the final object before returning it.

Examples:
โ€ข obj => JSON.stringify(obj) โ†’ Convert to JSON string
โ€ข obj => Object.keys(obj).length โ†’ Count properties
โ€ข obj => ({...obj, timestamp: Date.now()}) โ†’ Add timestamp
โ€ข obj => Object.entries(obj) โ†’ Convert to array of pairs

Useful for post-processing or format conversion.
Keys (Property Names) string
๐Ÿ’ก Type any text to see the result update live
๐ŸŽฏ Using special value โ€” click input to type instead
Test with:
Falsy
Truthy
Values string
getType()


Under the Hood

๐Ÿ“œ View Implementation Code
/**
 * Maps two arrays into an object, using keys array length as reference.
 * If a value is missing, assigns undefined. Extra values are ignored.
 *
 * Direct-mode only
 * @param {Array|string} data.keys - Array of property names, or comma-separated string
 * @param {Array|string} data.vals - Array of values, or comma-separated string
 * @param {string} [data.sep] - Separator for string splitting (default: ',')
 * @param {Function} [data.out] - Optional function to transform the final object
 *
 * @returns {Object} Object with keys and corresponding values (undefined if missing)
 *
 * @framework ggLowCodeGTMKit
 */
const getType = require('getType');
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

const separator = data.sep || ',';

// Convert to arrays if strings
const keysInput = data.keys;
const valsInput = data.vals;

const arrKeys = getType(keysInput) === 'array'
    ? keysInput 
    : (typeof keysInput === 'string' ? keysInput.split(separator) : []);

const arrValues =  getType(valsInput) === 'array'
    ? valsInput 
    : (typeof valsInput === 'string' ? valsInput.split(separator) : []);

// Build object
const result = {};
for (let i = 0; i < arrKeys.length; i++) {
    const key = arrKeys[i];
    // Skip empty keys
    if (key !== '' && key != null) {
        result[key] = i < arrValues.length ? arrValues[i] : undefined;
    }
}

return out(result);
๐Ÿงช View Test Scenarios (10 tests)
โœ… '[example] Pair keys and values'
โœ… String input with default separator
โœ… '[example] Custom separator'
โœ… Missing values - assigns undefined
โœ… Extra values - ignored
โœ… Empty key is skipped
โœ… Null key is skipped
โœ… Mix of array and string
โœ… Special characters in keys
โœ… Empty values array