๐๐๐๐๐๐ From Arrays Generator - Object โ GTM Variable Template for 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
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
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:
โข String:
โข Variable:
Notes:
โข Empty keys are skipped
โข Duplicate keys: last value wins
โข Special characters allowed:
Examples:
โข
โข
โข
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-idExamples:
โข
name,email,phoneโข
['firstName', 'lastName', 'age']โข
{{Form Field Names}}Values
๐พ Array of values or comma-separated string.
Accepted formats:
โข Array:
โข String:
โข Variable:
Notes:
โข Values matched to keys by position
โข Missing values โ
โข Extra values โ ignored
Examples:
โข
โข
โข
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:
Examples:
โข
โข
โข
โข
โข
๐ก For arrays, this parameter is ignored.
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:
โข
โข
โข
โข
Useful for post-processing or format conversion.
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 pairsUseful 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
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
getType()
Related Variables
Same category: GTM
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