javascript - Looping through a JSON Array gives "undefined" results -
this question has answer here:
- why using “for…in” array iteration bad idea? 22 answers
- javascript for…in vs for 21 answers
i have json string being parsed (in response var) ajax:
the json
{ "thearray":[ { "almostthere": { "whatwearelookingfor":"hello" } }, { "almostthere": { "whatwearelookingfor":"goodbye" } } ] } the json being parsed
var jsondata = json.parse(response); //response string version of json code! now, need loop json array, hereby mentioned thearray. this:
looping thearray
for (var contents in jsondata["thearray"]) { } and inside there, value of whatwearelookingfor element:
for (var contents in jsondata["thearray"]) { console.log(contents.whatwearelookingfor + "!"); } but there catch! console outputs... undefined!. - have tried multiple ways make work, such using contents["whatwearelookingfor"] , what-not, still same results.
you forgot access almostthere
jsondata.thearray[i].almostthere.whatwearelookingfor for (var = 0; < jsondata.thearray.length; i++) { console.log(jsondata.thearray[i].almostthere.whatwearelookingfor); }
Comments
Post a Comment