I have an array of arrays:
var json = {
"music": {
"albums": [{
"name": "foo",
"tracks": [{
"name": "foo",
"ref": "1"
}, {
"name": "bar",
"ref": "2"
}, {
"name": "foobar",
"ref": "3"
}]
}, {
"name": "bar",
"tracks": [{
"name": "f",
"ref": "1"
}, {
"name": "oo",
"ref": "2"
}, {
"name": "bar",
"ref": "3"
}]
}]
}
};
As you can see, album
is an array containing two arrays and a name
property. The arrays within album are called tracks
, which hold the tracks from the album displayed on the dom.
What I am trying to do is modify the dom with jquery, like this:
var gridListMusic = $('#grid-listMusic');
var gridListMusicList = $('#grid-listMusic ul');
function createAlbumSection(id) {
gridListMusic.append('<div>' + id + '</div>');
gridListMusic.append('<div class="grid-container">' + '<ul>' + '</ul>' + '</div');
$.each(json.music.albums.tracks, function (index, value) {
gridListMusicList.append('<li>' + value.name + '</li>');
});
}
function jsonDo(data) {
$.each(data.music.albums, function (index, value) {
alert(value.name);
if (typeof data.music.albums !== 'undefined') {
createAlbumSection(value.name);
}
});
}
This is expected to insert divs and uls into the DOM like this:
<div>foo</div>
<div class="grid-container">
<ul>
<li>foo</li>
<li>bar</li>
<li>foobar</li>
</ul>
</div>
<div>bar</div>
<div class="grid-container">
<ul>
<li>f</li>
<li>oo</li>
<li>bar</li>
</ul>
</div>
However, I am getting this error in my console as soon as it tries to insert the tracks
in the <ul>
:
Uncaught TypeError: Cannot read property 'albums' of undefined