You can try this-
const obj = {
"India" : {
"Karnataka" : ["Bangalore", "Mysore"],
"Maharashtra" : ["Mumbai", "Pune"]
},
"USA" : {
"Texas" : ["Dallas", "Houston"],
"IL" : ["Chicago", "Aurora", "Pune"]
}
};
const search = (obj, keyword) => {
return Object.values(obj).reduce((acc, curr) => {
Object.entries(curr).forEach(([key, value]) => {
if (value.indexOf(keyword) > -1) {
acc.push(key);
}
});
return acc;
}, []);
}
console.log(search(obj, 'Pune'));
This will search if the keyword
exists inside the array. If then push the key into the reduce
accumulator.
solved How to search for a value in Object which contains sub objects with as array values