assignObjectsFromPairs β GTM Variable Template for Object
assignObjectsFromPairs EXTENDED Object
Merges key-value pair objects from a GTM table.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβMerge pair objects
INPUT
Base Object: {name: 'John', age: 30}
ext: [{key: 'city', val: 'Paris'}, {key: 'country', val: 'France'}]
ext: [{key: 'city', val: 'Paris'}, {key: 'country', val: 'France'}]
OUTPUT
John
Override existing keys
INPUT
Base Object: {name: 'John', age: 30, city: 'London'}
ext: [{key: 'age', val: 35}, {key: 'city', val: 'Paris'}]
ext: [{key: 'age', val: 35}, {key: 'city', val: 'Paris'}]
OUTPUT
John
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
assignObjectsFromPairs
Base Object
πΎ The base object to merge into.
Supported formats:
β Object
Supported formats:
β Object
Properties to assign
πΎ An object whose properties will override or extend the base object.
Supported formats:
β Object
*** Merge pair objects***
*** Override existing keys***
Supported formats:
β Object
*** Merge pair objects***
*** Override existing keys***
KeyValue
β
β
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). 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
/** * Assigns properties from two objects into a new object. * * @param {Object} data.src - The base object. * @param {Object} data.ext - An object whose properties will override or extend the base object. * @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 merging. * * @returns {Object} A new object containing properties from both inputs. * * @framework ggLowCodeGTMKit */const makeTableMap = require('makeTableMap');
const assign = function(sourceObject, additionalObject) { // Copy all properties from additionalObject to sourceObject for (let key in additionalObject) { sourceObject[key] = additionalObject[key]; } return sourceObject;};
const toObjectPairs = function(simpleTable) { return makeTableMap(simpleTable, "key", "val");};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// assignObjectsFromPairs - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(assign(value, toObjectPairs(data.ext)));// ===============================================================================// assignObjectsFromPairs(...) β Apply Mode// ===============================================================================/*return function(value, additionalObject) { additionalObject = data.rp1 ? additionalObject : data.ext; return out(assign(value, additionalObject));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Merge pair objects'β
'[example] Override existing keys'β
Test with empty additional objectβ
Test with empty source objectβ
Test with mixed value types