简体   繁体   English

JavaScript-将键/值数据从动态键转换为动态键

[英]JavaScript - get key/value data from dynamic key into dynamic key

I have a var data that contains two nested dynamic key. 我有一个包含两个嵌套动态键的var数据。

I do not read eg content value of the key "key_1" 我不读取例如键“ 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 未捕获的TypeError: 无法读取未定义的属性“ 42”

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. 在我的代码中,var A是由函数中的数字变量提供的全局变量(productID)。

And var AA also corresponds to a variable (userID) from another file that is normally as a numeric variable. var AA还对应于另一个文件中的变量(用户ID),该文件通常是数字变量。

    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 ! 我真的看不出是什么问题!

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). '"' + 123456789 + '"'为您提供值'"123456789"' (请注意, 字符串的开头和结尾都用双引号引起来)。 You just want '123456789' . 您只想要'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"]); 

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. 您无需添加这些额外的引号,只会导致在对象中找不到类似'"123456789"'这样的字符串。

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

This is not necessarily the answer but looking at the way you wrote alert(data[A][AA]["key_1"]); 这不一定是答案,而是看您编写alert(data[A][AA]["key_1"]); , I do think it is important to provide more info. ,我确实认为提供更多信息很重要。

Point 1: -- key_1 第一点: key_1

If you needed to put the key key_1 in a variable you would have written: 如果需要将键key_1放入变量中,则可以这样写:

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'] ); alert(data ['123456789'] ['42'] ['key_1']);

So you can see why it has to be var A = '123456789'; 这样您就可以知道为什么它必须为var A = '123456789'; and var AA = '42'; 并且var AA = '42'; .

Point 2: -- Point of departure 点2: -出发点

Now, when it comes to dot notation, you cannot use dot notation on a key that starts with a digit [0-1]. 现在,当涉及dot符号时,您不能在以数字[0-1]开头的键上使用点符号。 Thus, you cannot write: 因此,您不能编写:

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

But you can write: 但是你可以这样写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM