toObjectPairs β GTM Variable Template for Object
toObjectPairs EXTENDED Object
Converts a GTM table of key-value pairs into an object.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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
Section titled β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
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled β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