I am trying to write a function that will retrieve a value by key, including searching in nested objects.
Here is as far as I got:
function getByKey (obj, key) {
if(obj.hasOwnProperty(key)){
return obj[key];
}else{
for(var prop in obj){
if(typeof prop == "object" && prop !== null){
if(prop.hasOwnProperty(key)){
return prop[key];
}else{
return iterate(prop, key);
}
}
}
}
}
If someone have this function ready and working or can fix my it will be great. If someone knows Underscore function that can do this it will be great.
nullis an object,iterateis missing.