Skip to content

⚡ OBJECT › From Arrays Generator — GTM Variable Template for Object

VARIABLES › OBJECT
⚡ OBJECT › From Arrays EXTENDED Object
Direct (.tpl)

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.


GTM Utilities

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


Pair keys and values
INPUT
Keys (Property Names): name,age,city
Values: John,30,Paris
Separator (optional): ,
OUTPUT
{"name":"John","age":30,"city":"Paris"}
Custom separator
INPUT
Keys (Property Names): name|age|city
Values: John|30|Paris
Separator (optional): |
OUTPUT
{"name":"John","age":"30","city":"Paris"}

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

⚡ OBJECT › From Arrays
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,[email protected],555-1234
['Alice', '[email protected]', '555-5678']
{{Form Field Values}}
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
Separator (optional) string
buildObjectFromArrays()


📜 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 buildObjectFromArrays = function(keysInput, valsInput, sep) {
const separator = sep || ',';
const arrKeys = getType(keysInput) === 'array'
? keysInput
: (typeof keysInput === 'string' ? keysInput.split(separator) : []);
const arrValues = getType(valsInput) === 'array'
? valsInput
: (typeof valsInput === 'string' ? valsInput.split(separator) : []);
const result = {};
for (let i = 0; i < arrKeys.length; i++) {
const key = arrKeys[i];
if (key !== '' && key != null) {
result[key] = i < arrValues.length ? arrValues[i] : undefined;
}
}
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
return out(buildObjectFromArrays(data.keys, data.vals, data.sep));
🧪 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