zipObject β GTM Variable Template for Object
zipObject EXTENDED Object
Creates an object from parallel arrays of keys and values.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβZip keys and values
INPUT
Keys Array: ['name', 'age', 'city']
Values Array: ['John', 30, 'Paris']
Values Array: ['John', 30, 'Paris']
OUTPUT
{name: 'John', age: 30, city: 'Paris'}
Empty arrays return empty
INPUT
Keys Array: []
Values Array: []
Values Array: []
OUTPUT
{}
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
zipObject
Keys Array
πΎ Array of property names that will become object keys.
Supported formats:
β Array: ["name", "age", "city"]
Supported formats:
β Array: ["name", "age", "city"]
Values Array
πΎ Array of values corresponding to each key. If shorter than keys array, remaining keys will have undefined values.
Supported formats:
β Array: ["John", 30, "Paris"]
Supported formats:
β Array: ["John", 30, "Paris"]
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the keys array before creating the object (e.g., normalize keys, filter out invalid keys).
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result object before returning it (e.g.,
obj => JSON.stringify(obj), obj => Object.freeze(obj)). Useful for chaining transformations on the output.Keys Array array
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Values Array array
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
zipObject()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Creates an object from two arrays: one of property names and one of corresponding values. * * @param {Array} data.src - Array of property names (keys). * @param {Array} data.val - Array of corresponding values. * @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 processing. * * @returns {Object} An object composed of the given keys and values. * * @framework ggLowCodeGTMKit */const getType = require('getType');
const zipObject = function(keys, values) { if (getType(keys) !== 'array' || getType(values) !== 'array') { return {}; }
const result = {}; const length = keys.length;
for (let i = 0; i < length; i++) { const key = keys[i]; if (key !== null && key !== undefined) { result[key] = i < values.length ? values[i] : undefined; } }
return result;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// zipObject - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(zipObject(value, data.val));// ===============================================================================// zipObject(...) β Apply Mode// ===============================================================================/*return function(keys, values) { values = data.rp1 ? values : data.val; return out(zipObject(keys, values));};*/π§ͺ View Test Scenarios (10 tests)
β
'[example] Zip keys and values'β
Test with values array shorter than keysβ
Test with keys array shorter than valuesβ
'[example] Empty arrays return empty'β
Test with null key (should skip)β
Test with undefined key (should skip)β
Test with non-array keys returns empty objectβ
Test with non-array values returns empty objectβ
Test with mixed value typesβ
Test with duplicate keys (last one wins)