Q:

Recursively searching for a property in object

I have an Object say obj.I need to search for a property obj.property if its not there(undefined) search in obj.parent.property .If its not there search in obj.parent.parent.property and so on.. until I get that property
like this..
obj.property                        [undefined]
obj.parent.property                 [undefined]
obj.parent.parent.property          [undefined]
obj.parent.parent.parent.property   [found] .Terminate here.

A:

You could use a recursive approach by checking if the property exist, otherwise call iter again with the parent key.

function iter(object) {
    return 'property' in object ? object.property : iter(object.parent);
}

var object = { parent: { parent: { parent: { parent: { property: 42 } } } } };

console.log(iter(object));

