Skip to content

toObjectPairs β€” GTM Variable Template for Object

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

Converts a GTM table of key-value pairs into an object.



Table to object map
INPUT
Map-like structure to create: [{key: 'name', val: 'John'}, {key: 'age', val: 30}, {key: 'city', val: 'Paris'}]
OUTPUT
{name: 'John', age: 30, city: 'Paris'}
Duplicate keys last wins
INPUT
Map-like structure to create: [{key: 'color', val: 'red'}, {key: 'size', val: 'large'}, {key: 'color', val: 'blue'}]
OUTPUT
{color: 'blue', size: 'large'}

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

Read-only Preview
toObjectPairs
Map-like structure to create
πŸ’Ύ The array of objects representing the simple table.

Supported formats:
  βœ“ Array of Objects

*** Table to object map***

*** Duplicate keys last wins***
Property KeyProperty Value
βŠ–
βŠ–
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the table before internal logic (e.g., filter rows, normalize data). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., obj => JSON.stringify(obj), obj => Object.keys(obj).length for count). Useful for chaining transformations on the output.


πŸ“œ View Implementation Code
/**
* Converts a simple table object into a Map based on the specified key and value fields.
*
* @param {Array<Object>} data.tbl - The array of objects representing the simple table, where each object has a 'key' and 'value' field.
* @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 tbl before conversion.
*
* @returns {Object|null} Returns an object (Map) where the specified key and value fields become the keys and values in the Map. If no valid key-value pairs are found, it returns null.
*
* @framework ggLowCodeGTMKit
*/
const makeTableMap = require('makeTableMap');
const toObjectPairs = function(simpleTable) {
return makeTableMap(simpleTable, "key", "val");
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toObjectPairs - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedTable = applyCast(data.pre, data.tbl);
return out(toObjectPairs(processedTable));
// ===============================================================================
// toObjectPairs() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(toObjectPairs(value));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Table to object map'
βœ… Test with single key-value pair
βœ… '[example] Duplicate keys last wins'
βœ… Test with empty array returns null
βœ… Test with mixed value types