2

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Sebastian Olsen
  • 10,318
  • 9
  • 46
  • 91

2 Answers2

0

instead of

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>');
        });
    }

use

function createAlbumSection(id) {
        gridListMusic.append('<div>' + id + '</div>');
        gridListMusic.append('<div class="grid-container">' + '<ul>' + '</ul>' + '</div');
        $.each(json.music.albums[0].tracks, function (index, value) {
            gridListMusicList.append('<li>' + value.name + '</li>');
        });
    }
jrath
  • 980
  • 4
  • 14
0

You should rewrite your createAlbumSection to accept the list of tracks:

function createAlbumSection(id, tracks) {
    var html = '<div>' + id + '</div>' + '<div class="grid-container">' + '<ul>';

    $.each(tracks, function (index, value) {
        html += '<li>' + value.name + '</li>';
    });

    html += '</ul>' + '</div>';

    gridListMusic.append(html);
}

Then, send it in with your jsonDo:

function jsonDo(data) {
    $.each(data.music.albums, function (index, value) {
        alert(value.name);
        if (typeof data.music.albums !== 'undefined') {
            createAlbumSection(value.name, value.tracks);
        }
    });
}
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • You mean the `
      ` within `.grid-container`?
    – Dave Chen Jul 18 '15 at 21:05
  • Yes. It's only inserting the `
      `, not the `
    • `'s
    – Sebastian Olsen Jul 18 '15 at 21:06
  • @SebastianOlsen I've edited my answer, it should insert the `
  • ` elements within the `
      `.
  • – Dave Chen Jul 18 '15 at 21:09
  • Much appreciated. I didn't know this question was a duplicate, I couldn't find it on the search, but then again I didn't know nested was a word. Thank you so much for helping me! – Sebastian Olsen Jul 18 '15 at 21:12
  • That's weird, I never unchecked it. Oh well, I checked it again. :) – Sebastian Olsen Jul 19 '15 at 10:44