简体   繁体   English

如何使用变量作为索引从Javascript中的JSON数组中获取特定值?

[英]How to use a variable as an index to get a specific value out of a JSON array in Javascript?

I simply want to pinpoint a specific value out of a JSON array. 我只是想从JSON数组中找出一个特定的值。

Sample of JSON array: JSON数组示例:

{
    "00002": {
        "Job Number": "00002",
        "Company": "Corporate",
        "Supervisor": "Great Person",
        "Date Input": "2016-01-07"
    },

    "00003": {
        "Job Number": "00003",
        "Company": "SmallGuy",
        "Supervisor": "Awful Person",
        "Date Input": "2012-03-05"
    }
}

This works in Javascript: 这适用于Javascript:

alert(javascript_array["00002"].Company);

But I want use a dynamic variable to call a record, like this: 但我想使用动态变量来调用记录,如下所示:

var my_variable = 00002;

//OR I've tried:

var my_variable = "'"+00002+"'";

alert(javascript_array[my_variable].Company); //DOES NOT WORK. UNDEFINED??

No matter what I do, I can't use a variable mid-array call. 无论我做什么,我都不能使用可变的中阵调用。

Help please! 请帮助!

Use the string as key. 使用字符串作为键。

var my_variable = '00002';

 var object = { "00002": { "Job Number": "00002", "Company": "Corporate", "Supervisor": "Great Person", "Date Input": "2016-01-07" }, "00003": { "Job Number": "00003", "Company": "SmallGuy", "Supervisor": "Awful Person", "Date Input": "2012-03-05" } } my_variable = '00002'; document.write(object[my_variable].Company); 

For getting all keys from the object, you can use Object.keys() : 要从对象获取所有键,可以使用Object.keys()

 var object = { "00002": { "Job Number": "00002", "Company": "Corporate", "Supervisor": "Great Person", "Date Input": "2016-01-07" }, "00003": { "Job Number": "00003", "Company": "SmallGuy", "Supervisor": "Awful Person", "Date Input": "2012-03-05" } }, keys = Object.keys(object); keys.forEach(function (k) { document.write(object[k].Company + '<br>'); }); 

Your key is a string, but your variable isn't, so there's no match. 你的密钥是一个字符串,但你的变量不是,所以没有匹配。 Just use this: 只要用这个:

var my_variable = "00002";

To access key items for a JSON object you need to use strings, but if not it will attempt to convert the value into a string using .toString() . 要访问JSON对象的关键项,您需要使用字符串,但如果不是,它将尝试使用.toString()将值转换为字符串。 For your first case you are attempting to define a number: 对于您的第一种情况,您尝试定义一个数字:

var my_variable = 00002;

Although 00002 isn't a valid value for a number, as such it will contain the value 2 , and converted to a string it's "2" . 虽然00002不是数字的有效值,但它将包含值2 ,并转换为字符串,它是"2" In your JSON there is no such javascript_array["2"] . 在你的JSON中没有这样的javascript_array["2"] The second case has a similar problem: 第二种情况有类似的问题:

"'"+00002+"'" => "'"+2+"'" => "'2'"

Also there is not such javascript_array["'2'"] , along with this you are adding unneeded quotes '...' . 还有没有这样的javascript_array["'2'"] ,以及你添加不需要的引号'...' In this case ( as others pointed out ) just define my_variable as a string with the value "00002" . 在这种情况下( 正如其他人指出的那样 )只需将my_variable定义为值为"00002"的字符串。

var my_variable = "00002";

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

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