I have an array of keys
ex: var keyarray = [k1, k2, k3, ... ,kn];
Let o be an object;
I need to check the value of o[k1][k2][k3]...[kn]
I have tried the below methods:
Approach 1:
- Convert each element of keyArray into array so I get the below output var keyarray = [[k1], [k2], [k3], .... ,[kn]]
- Then execute keyarray.join(''); --> This results in a string such as k1k2k3...kn
Approach 2:
- Convert each element of keyArray into array so I get the below output var keyarray = [[k1], [k2], [k3], .... ,[kn]]
- Loop through the keyarray and append each value to a string so I get the below output s = "[k1][k2][k3]...[kn]"
- And then try o[s] --> returns undefined.
Looking for any kind of suggestions.
Edit: Adding a real time example for proper understanding.
I have JSON object like below
obj = {
"history": [
{
"at": "someval",
"by": "someval"
}.
{
"at": "someval",
"by": "someval"
}
]
}
For the above I will have a keyArray like below
var keyArray = ["history", "0", "at"],
To access this I have to dynamically generate an object like below
obj[history][0][at]
Hope this helps.