1

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 !

3 Answers 3

1

Don't put quotes in your property names. '"' + 123456789 + '"' gives you the value '"123456789"' (note that double quotes at the beginning and end, which are actually in the string). You just want '123456789'.

So:

var A = '123456789';
var B = '987654321';

var AA = '42';

Live Example:

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"]);

For ones of these without leading zeros, you could even do away with the quotes entirely and use numbers (which would get coerced to string by []).

Live Example:

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"]);

Sign up to request clarification or add additional context in comments.

Comments

0

Replace:

var A = '"' + 123456789 + '"';
var B = '"' + 987654321 + '"';
var AA = '"' + 42 + '"';

With:

var A = '' + 123456789;
var B = '' + 987654321;
var AA = '' + 42;

Or better yet, just declare them as strings:

var A = '123456789';
var B = '987654321';
var AA = '42';

You don't need to add those extra quotes, that will only result in strings like '"123456789"', which aren't found in the object.

Then it will work:

Live Example:

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"]);

Comments

0

This is not necessarily the answer but looking at the way you wrote alert(data[A][AA]["key_1"]);, I do think it is important to provide more info.

Point 1: -- key_1

If you needed to put the key key_1 in a variable you would have written:

var AAA = 'key_1';
alert( data[A][AA][AAA] );

and that would have been correct. Now, you can go ahead and apply the same reasoning with the other keys. Visualize:

alert( data['123456789']['42']['key_1'] );

So you can see why it has to be var A = '123456789'; and var AA = '42';.

Point 2: -- Point of departure

Now, when it comes to dot notation, you cannot use dot notation on a key that starts with a digit [0-1]. Thus, you cannot write:

alert( data.123456789.42.key_1 ); //BAD

But you can write:

alert( data['123456789']['42'].key_1 ); //GOOD

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.