I have a var data that contains two nested dynamic key.
I do not read eg content value of the key "key_1"
var A = '"' + 123456789 + '"';
var B = '"' + 987654321 + '"';
var AA = '"' + 42 + '"';
var data = {
"123456789":{
"42":{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3"
},
"DYNAMIC_KEY_2":{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3"
},
"DYNAMIC_KEY_3":{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3"
}
},
"987654321":{
"DYNAMIC_KEY_1":{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3"
},
"DYNAMIC_KEY_2":{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3"
},
"DYNAMIC_KEY_3":{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3"
}
}
}
alert(data[A][AA]["key_1"]);
Uncaught TypeError: Cannot read property '"42"' of undefined
I tried several solutions, but it did not !
Could someone explain me how to proceed ? Thanks :)
--- UPDATE ---
Very strange, applying your advice, I still have the same error ...
In my code, the var A is a global variable (productID) fed by a numeric variable in a function.
And var AA also corresponds to a variable (userID) from another file that is normally as a numeric variable.
var room;
function ioJoin(Mid){
room = Mid;
var _localuserid = LS.wpbp.id;
var trackdata = {};
var users = {};
users[_localuserid] = {
active: true,
time: $.now(),
user: LS.wpbp.id,
productID: Mid
}
trackdata[Mid] = users
socket.emit('send:newuser', trackdata);
}
socket.on("load:joinroom", function(data) {
var _localuserid = LS.wpbp.id;
// room & _localuserid are numbers
alert(data[room][_localuserid]["active"]);
});
I really do not see what the problem is !