hasNested β GTM Variable Template for Object
hasNested EXTENDED Object
Checks if a nested property exists in an object using a path.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβNested property exists
INPUT
Object to Check: {a: {b: {c: 'value'}}}
Property Path: a.b.c
Property Path: a.b.c
OUTPUT
true
Nested property missing
INPUT
Object to Check: {a: {b: {}}}
Property Path: a.b.c
Property Path: a.b.c
OUTPUT
false
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
hasNested
Object to Check
πΎ The object to check for the nested property.
Supported formats:
β Object variable: {{myObject}}
β Object literal
Supported formats:
β Object variable: {{myObject}}
β Object literal
Property Path
πΎ The path to the nested property. Can use dot notation or array format.
Supported formats:
β String (dot notation): "user.profile.name"
β Array: ["user", "profile", "name"]
Supported formats:
β String (dot notation): "user.profile.name"
β Array: ["user", "profile", "name"]
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the object before checking (e.g., normalize object structure, parse JSON).
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the boolean result before returning it (e.g.,
val => !val to invert, val => val ? 'exists' : 'missing'). Useful for chaining transformations on the output.Object to Check object
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Property Path array
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
hasNested()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Checks if a nested property exists in an object using a path string or array. * * @param {Object} data.src - The object to check. * @param {string|string[]} data.pth - The path to the property (e.g., "a.b.c" or ["a", "b", "c"]). * @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 checking. * * @returns {boolean} Returns true if the nested property exists, false otherwise. * * @framework ggLowCodeGTMKit */const getType = require('getType');
const hasNested = function(obj, path) { if (getType(obj) !== 'object' || obj === null) { return false; }
const pathParts = getType(path) === 'array' ? path.map(function (part) { return part.toString(); }) : path.split('.');
let current = obj; let index = 0; const length = pathParts.length;
while (current != null && index < length) { if (!current.hasOwnProperty(pathParts[index])) { return false; } current = current[pathParts[index++]]; }
return index === length;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// hasNested - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(hasNested(value, data.pth));// ===============================================================================// hasNested(...) β Apply Mode// ===============================================================================/*return function(obj, path) { path = data.rp1 ? path : data.pth; return out(hasNested(obj, path));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Nested property exists'β
'[example] Nested property missing'β
Test with array path formatβ
Test property exists with undefined valueβ
Test with non-object input returns false