从数组中的数组项中获取json项

时间:2015-07-18 20:48:33

标签: javascript jquery arrays dom

我有一个数组数组:

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"
            }]
        }]
    }
};

如您所见,album是一个包含两个数组和name属性的数组。专辑中的数组称为tracks,用于保存dom上显示的专辑中的曲目。 我想要做的是用jquery修改dom,如下所示:

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

预计会将div和uls插入DOM中,如下所示:

<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>

但是,一旦尝试在tracks中插入<ul>,我就会在控制台中收到此错误:

  

未捕获的TypeError:无法读取属性&#39;相册&#39;未定义的

2 个答案:

答案 0 :(得分:0)

而不是

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

答案 1 :(得分:0)

您应该重写createAlbumSection以接受曲目列表:

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

然后,将其与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);
        }
    });
}