For a simple object:
const simpleObject = {
"a": "aaa" ,
"b": "bbb" ,
};
I know how to get the key of a value:
Object.keys(simpleObject).find(key => simpleObject[key] === "aaa");
However, if I have a complex object like this:
const complexObject = {
"A": [ "a1", {"a2": ["a21", "a22" ]}, "a3"], //compact representation
"B": [ //expanded representation
"b1",
"b2",
{"b3": [
"b31",
"b32"
]
}
],
};
How to return the key of a value as deep as possible? For example:
Input | Output |
---|---|
A |
A |
a1 |
A |
a2 |
A |
a21 |
A.a2 |
I think the first step is to get the value list of all the keys:
valueList = Object.values(map1);
then using some kind of loop. But I don't know how to work it out.
In best scenario the pattern of the object can be go on, but if that's too hard then stopping the recursion at level 2 is fine.
I need a solution that works on plain JS (I'm making an automation script for Fibery). But if there is a framework I'm happy to know.