Javascript of property path in object I am trying to find the path of property in an object in JavaScript.  For example, given

var myObj = { 'a': {'b': {'c': {'x': 1, 'y': 2 }}} }

the path of 'x' in myObj would be a.b.c.x (dot notation).

I wrote the code below using a recursive mechanism.

function findPropPath(obj, name, currentPath) {
    var currentPath = currentPath || '';

    for (var i in obj)
    {
        if (i == name)
        {
            return name;
        }
        else if (obj[i] &amp;&amp; typeof obj[i] == "object")
        {
            var result = findPropPath(obj[i], name, currentPath + '.' + i);
            if (result == name)
            {
                return currentPath += '.' + name;
            }
            else if (result &amp;&amp; result.endsWith('.' + name)) {
                return result;
            }
        }
    }
}


It works as expected, I would like to know if it can be optimized or other better ways find property path. Also I assume the call stack size would grow for  enormous object. Any review would be of great help!
Please pick a consistent brace style.

i has the connotation of being an integer counter.  I would pick a different variable name for iterating over the properties of an object.

I don't see any need for obj[i] &amp;&amp; in the else if test. 

If you use recursion properly, you shouldn't need to have a third parameter at all.



function findPropPath(obj, name) {
    for (var prop in obj) {
        if (prop == name) {
            return name;
        } else if (typeof obj[prop] == "object") {
            var result = findPropPath(obj[prop], name);
            if (result) {
                return prop + '.' + result;
            }
        }
    }
    return null;    // Not strictly needed, but good style
}

var myObj = {'a': {'b': {'c': {'x': 1, 'y': 2 }}}};
console.log(findPropPath(myObj, 'x'));