Apologies if this question has already been addressed elsewhere, but I couldn't find it. I have a JSON object with dynamic keys:
{
"main": [
{
"Example1": [
{
"Example1_Var02": "5",
"Example1_Var09": "443",
"Example1_Var27": "23",
"Example1_Var01": "31"
}
],
"Example2": [
{
"Example2_Var99": "2",
"Example2_Var84": "344",
"Example2_Var46": "56",
"Example2_Var03": "78"
}
]
}
]
}
These key names are unknown to me and may change frequently, except for the main key. After a successful $.ajax()
call that fetches this data (e.g. success: function(data) {
), I want to store each nested array in local storage without having to know their specific keys. However, my attempts to do this using known sub-array keys are not working. For example:
for(var key in data){
localStorage.setItem(key.Example1[0], JSON.stringify(data[key.Example1]));
}
results in an error "Uncaught TypeError: Cannot read property '0' of undefined". Since I can't achieve this with known sub-array keys, I'm finding it challenging to handle this at a higher level of abstraction. How can I store all nested arrays within "main" separately in local storage even when their names are unknown in the JSON? Thank you for your assistance.