Reputation: 31732
The below code works just fine except for the annoying undefined I am getting when appending elements dynamically retrieved from a JSON multidimensional array. I could not figure out from where it is coming, however, I assume it is coming from declaring a variable outside function and using it inside $.each()
to accumulate data.
var c = 0;
var q = 1;
$.each(json, function (i, data) {
var answers; // declared here and outside this function - same results.
$.each(data.answers, function (i, a) {
answers += '<tags>' + a + '</tags>'; // the problem is here "maybe".
});
$('.foo').append(answers); // I get "undefined" ahead of values retrieved.
});
Upvotes: 0
Views: 265
Reputation: 4591
You get 'undefined' ahead of your results because you use +=
and start with an undefined answers
variable.
Try declaring var answers = '';
.
Upvotes: 10