can you help me to make a function that will be able to find parent object key by child object value?
This is object structure:
const mainObject = {
FIRST: {
key1: 'value1',
key2: 'value2',
},
SECOND: {
key3: 'value3',
key4: 'value4',
},
};
I tried to use this function:
function getKeyByValue(object, value) {
return Object.keys(object).find((key) => object[key] === value);
}
but if I run getKeyByValue(mainObject, 'value4') it returns undefined.
Can someone help how I can improve getKeyByValue function to get the parent key (I need to get 'SECOND' name)?