If I have an object with nested properties. Is there a function that will search all properties, and the properties with values that are other objects (which also have their own properties) and so forth?
Example object:
const user = {
id: 101,
email: '[email protected]',
info: {
name: 'Please',
address: {
state: 'WX'
}
}
}
In the object above is there a way I could simply call something like
console.log(findProp(user, 'state'));
console.log(findProp(user, 'id'));
What you need is a recursive function which looks up nested items (Object and Array as well) for the matching key (i also added an array for the lookup):
Object.hasOwnPropertyguards against iterating over or retrieving built-in properties.