1

I simply want to pinpoint a specific value out of a JSON array.

Sample of JSON array:

{
    "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:

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!

4
  • 1
    What is my_variable ? Works here: jsfiddle.net/wsq3yj30 Commented Mar 31, 2016 at 8:21
  • Sorry, I just made an edit. Ideally, "my_variable" would be 00002, 00003, up to 99999 really... Commented Mar 31, 2016 at 8:22
  • Just do: var my_variable = "00002", you are adding unneeded quotes ("'00002'"), which is why it can't find it. Commented Mar 31, 2016 at 8:23
  • Key variable must be a String, else, its toString() value will be used as key name. Commented Mar 31, 2016 at 8:35

3 Answers 3

2

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():

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>');
});

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

Comments

1

Your key is a string, but your variable isn't, so there's no match. Just use this:

var my_variable = "00002";

Comments

0

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(). 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". In your JSON there is no such 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 '...'. In this case (as others pointed out) just define my_variable as a string with the value "00002".

var my_variable = "00002";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.