I am appending a json response to an html element. The code is very simple
HTML
<div>
<ul id='grid'></ul>
</div>
jQuery
data = ["Paintings",{ "age":0}]
$(function() {
$.each(data, function(i,index) {
$('div').find('ul').append('<li>'+index.age+'</li>');
});
});
The output looks like this
undefined
0
Where is the undefined coming from??
I tried using this to remove the undefined
$('#grid').children(':first-child').remove('li');
but it removes the “0” and the undefined.
datais an array, the first element of array is a stringPaintings. Second element of array is an object with propertyage.The
eachloops over both elements in array and since string has no propertyageit creates an LI with textundefined.You could change the data structure or look for
objectorstringin array and react accordingly…not sure what you want to do. Not clear what you are doing withPaintings