If I have a dictionary that looks like this:
const test = {
"auth": {
"username": "UserABC",
"password": "abc123!"
}
}
to access the username I would simply use test.auth.username
to get "UserABC".
My question is how can I, if it's at all possible, pass 'auth.username' as a function to a variable. For example, if the variable test
is part of a class and I only want to use getters and setters to alter any key/value pairs, how can I write a getter and setter to reference a nested dictionary object?
If I do something like
function getValue(key) {
return test[key]
}
then I can get the username by calling getValue('auth')['username']
, but this is a bit ugly for dictionaries that have more that just two levels. Also, this wouldn't solve my setter function problem.
How could I do something along the lines of getValue('auth.username');
?