0
            {
            "wordsacross": [
                {"ACHE": [
                    { "letter":"A" , "square":"A1" }, 
                    { "letter":"C" , "square":"A2" }, 
                    { "letter":"H" , "square":"A3" },
                    { "letter":"E" , "square":"A4" }
                ]},
                {"OPT": [
                    { "letter":"O" , "square":"A6" }, 
                    { "letter":"P" , "square":"A7" }, 
                    { "letter":"T" , "square":"A8" }
                ]}
            ],
            "wordsdown": [
                {"ALPHA": [
                    { "letter":"A" , "square":"A1" }, 
                    { "letter":"L" , "square":"B1" }, 
                    { "letter":"P" , "square":"C1" },
                    { "letter":"H" , "square":"D1" },
                    { "letter":"A" , "square":"E1" }
                ]},
                {"BRO": [
                    { "letter":"B" , "square":"G1" }, 
                    { "letter":"R" , "square":"H1" }, 
                    { "letter":"O" , "square":"I1" }
                ]}
            ]
            }

            $.ajax({
                type: "POST",
                url: "query.words.php",
                data: { puzzleid: vPuzzleId },
                async: false
            }).done(function( msg ) {
                vWords = JSON.parse( msg );
                console.log(vWords);
                console.log("There are "+vWords["wordsacross"].length+" words across");
                for(var i=0;i<vWords["wordsacross"].length;i++)
                {
                    console.log( vWords["wordsacross"][i].length );
                    console.log( vWords["wordsacross"][i][0]["square"] );
                }
            });

すべての正方形のアイテムの内容をコンソールに出力しようとしています。console.log での私の試みは両方とも未定義です。各正方形にアクセスしてコンソールに出力するにはどうすればよいですか?

前もって感謝します...

4

4 に答える 4

1

http://jsfiddle.net/xp8Ww/

for(var i=0;i<vWords["wordsacross"].length;i++)
            {
                var keys =Object.keys(vWords["wordsacross"][i]);
                console.log(keys.length);
                for(var j=0;j<keys.length;j++){
                    var keys2=vWords["wordsacross"][i][keys[j]].length;
                    for(var k=0;k<keys2;k++){
                      console.log(vWords["wordsacross"][i][keys[j]][k]["square"]);
                    }
                }

            }
于 2013-08-19T04:36:31.743 に答える