Skip to content

zipObject β€” GTM Variable Template for Object

VARIABLES β€Ί OBJECT
zipObject EXTENDED Object
Direct (.tpl) Apply (.tpl)

Creates an object from parallel arrays of keys and values.



Zip keys and values
INPUT
Keys Array: ['name', 'age', 'city']
Values Array: ['John', 30, 'Paris']
OUTPUT
{name: 'John', age: 30, city: 'Paris'}
Empty arrays return empty
INPUT
Keys Array: []
Values Array: []
OUTPUT
{}

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"]
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"]
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
zipObject()


πŸ“œ 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)