user3401408
user3401408

Reputation:

Getting inconsistent output on iterating over json

set_options_list = function(selctelm, json){
    $(selctelm).empty();
    for ( key in json.array.F_02_0010){
        val = json.array.F_02_0010[key]
        console.log(key, typeof val, val[0], val[1]);
    };
}

Here is my json array:

json.array = {F_02_0010 : [{0 : "-------"},
                      {1:"20億円以上"},
                      {2:"14億円以上20億円未満"},
                      {3:"7億円以上14億円未満"},
                      {4:"7000万円以上7億円未満"},
                      {5:"7000万円未満"}],

          F_02_0110 : [{0 : "-------"},
                      {1:"10億円以上"},
                      {2:"7億円以上10億円未満"},
                      {3:"4億円以上7億円未満"},
                      {4:"4000万円以上4億円未満"},
                      {5:"4000万円未満"}],

          F_02_0210 :[{0 : "-------"},
                      {1:"10億円以上"},
                      {2:"7億円以上10億円未満"},
                      {3:"4億円以上7億円未満"},
                      {4:"5000万円以上4億円未満"},
                      {5:"5000万円未満"}],
         "default": [{0 : "-------"}]
};

I'm getting undefined to access key and value.What could be the reason? Here is console.log result:

0 object ------- undefined 
1 object undefined 20億円以上 
2 object undefined undefined 
3 object undefined undefined 
4 object undefined undefined 
5 object undefined undefined 

I'm not sure why I'm getting undefined here?

Upvotes: 1

Views: 62

Answers (3)

Maurice Perry
Maurice Perry

Reputation: 32831

Makes sense:

json.array.F_02_0010[0] is {0 : "-------"},

json.array.F_02_0010[1] is {1:"20億円以上"},

json.array.F_02_0010[2] is {2:"14億円以上20億円未満"},

etc...

So:

json.array.F_02_0010[0][i] is "-------" if i == 0, undefined otherwise

json.array.F_02_0010[1][i] is "20億円以上" if i == 1, undefined otherwise

json.array.F_02_0010[2][i] is "14億円以上20億円未満" if i == 2, undefined otherwise

etc...

Upvotes: 0

Roman
Roman

Reputation: 6418

you probably want it this way:

json.array = {F_02_0010 : ["-------",
                      "20億円以上",
                      "14億円以上20億円未満",
                      "7億円以上14億円未満",
                      "7000万円以上7億円未満",
                      "7000万円未満"],

          F_02_0110 : ["-------",
                      "10億円以上",
                      "7億円以上10億円未満",
                      "4億円以上7億円未満",
                      "4000万円以上4億円未満",
                      "4000万円未満"],

          F_02_0210 :["-------",
                      "10億円以上",
                      "7億円以上10億円未満",
                      "4億円以上7億円未満",
                      "5000万円以上4億円未満",
                      "5000万円未満"],
         "default": ["-------"]
};

OR this way:

json.array = {F_02_0010 : {0 : "-------",
                      1:"20億円以上",
                      2:"14億円以上20億円未満",
                      3:"7億円以上14億円未満",
                      4:"7000万円以上7億円未満",
                      5:"7000万円未満"},

          F_02_0110 : {0 : "-------",
                      1:"10億円以上",
                      2:"7億円以上10億円未満",
                      3:"4億円以上7億円未満",
                      4:"4000万円以上4億円未満",
                      5:"4000万円未満"},

          F_02_0210 : {0 : "-------",
                      1:"10億円以上",
                      2:"7億円以上10億円未満",
                      3:"4億円以上7億円未満",
                      4:"5000万円以上4億円未満",
                      5:"5000万円未満"},
         "default": {0 : "-------" }
};

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94449

The F_02_0010 property holds an array of objects, which must be iterated using traditional for syntax. During each iteration grab the object then iterate through its single key:

set_options_list = function(selctelm, json){
    $(selctelm).empty();
    for (var i =0; json.array.F_02_0010.length; i++){
        var val = json.array.F_02_0010[i];

        for(key in val){
            console.log(val[key]);
        }         

    };
}

Upvotes: 0

Related Questions