toObjectPairs — GTM Variable Template for Object
toObjectPairs EXTENDED Object
Converts a GTM table of key-value pairs into an object.
Examples
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'}
GTM Configuration
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***
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.
Related Variables
Same category: Object
Under the Hood
📜 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