Skip to content

hasNested — GTM Variable Template for Object

VARIABLES › OBJECT
hasNested EXTENDED Object

Checks if a nested property exists in an object using a path.



Examples

Nested property exists
INPUT
Object to Check: {a: {b: {c: 'value'}}}
Property Path: a.b.c
OUTPUT
true
Nested property missing
INPUT
Object to Check: {a: {b: {}}}
Property Path: a.b.c
OUTPUT
false

GTM Configuration

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
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"]
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
hasNested()


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 ? data.pth : path;
   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