Let's say I have the following object:
const obj = {
'myApi': ['keyOne', 'keyTwo'],
'myApi.keyOne': ['three', 'four'],
'myApi.keyTwo': [...],
'myApi.keyOne.three': [...]
'myApi.keyOne.four': [...]
}
Now, based on the following string:
const str = "if(myApi.keyOne.three"
I want to match the correct object key, but from right to left. So, in the above example, I want to get obj["myApi.keyOne.three"]
.
indexOf
or str.test
methods will not work because they will catch myApi
and myApi.keyOne
also.
Note that it could be any string, the str
is just an example. For example:
while(myApi.keyOne.three) {} // should match obj["myApi.keyOne.three"]
if(myApi.keyOne) {} // should match obj["myApi.keyOne"]
etc.
How can I do this?