Skip to content

zipObject — GTM Variable Template for Object

VARIABLES › OBJECT
zipObject EXTENDED Object

Creates an object from parallel arrays of keys and values.



Examples

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
{}

GTM Configuration

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()


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 ? data.val : values;
   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)