I need to iterate over a nested function and find the sub-object that has the same key as I want. here is the code:
const searchObject = (obj, label) => {
const object = Object.keys(obj).forEach(key => {
if(label === key) {
return obj[key];
} else if(typeof obj[key] === "object") {
const value = searchObject(obj[key], label);
if(value) return value;
}
});
return object;
};
I searched a lot and I found many people are recommending this way but I dont know why I get undefined when I log console.log(searchObject(obj, "Intercept")). (I am using React framework)
.forEach. That won't work.