My goal: create a way to get a property's value from a multi-level javascript object.
For example, here is an object I'd like to get a property's value from:
var originalCookieObject = {
        User: "notmyemail@gmail.com",
        GridStates: {
            gridName : {
                "page": 1,
                "pageSize": 50,
                "sort": [
                            {
                                field: "CommonName",
                                dir: "desc"
                            }
                        ]
            }
        },
        ProtectionState: {
            SelectedRow: "string",
            PanelsOpen: ['string', 'string']
        }
    }
And here is how I might retrieve the property's value:
getProperty(originalCookieObject, 'gridName');
I've tried the following recursive function so far:
function traverse(toTraverse, findKey) {
    if( typeof toTraverse == "object" ) {
        var foundValue = toTraverse[findKey];
        if (foundValue){ return foundValue; }
        $.each(toTraverse, function(k,v) {
            // k is either an array index or object key
            traverse(v, findKey);
        });
    }
}
var objectValue = traverse(originalCookieObject, 'gridName');
console.log(objectValue);
My issue is when I try to go more than one level deep, that current traverse() call only returns the property value to the parent traverse() call.
My questions:
- How could I make a child traverse()call return the property value to the parenttraverse()function?
- Is there a better way to do this?
 
     
     
    