0

我整天都在尝试解决这个问题:

我有一个带有子目录的目录,fe:

 | Music Artist 1
 | - Album Nr 1
 | -- Track 1
 | -- Track 2
 | -- ...
 | - Album Nr 2
 | -- Track 1
 | -- Track 2
 | -- ...
 | Music Artist 2
 | - Album Nr 1
 | -- Track 1
 | -- Track 2
 | -- ...

现在,我将遍历这些目录——将所有细节添加到数组/对象中。所以它应该是这样的:

 [ { artist: Music Artist 1, album { title: Album Nr1, songs: { title: Track 1 } ... } ]

获取所有目录名称/文件不是问题。我只是不知道如何创建数组:(

先谢谢了!

编辑:这是我的尝试: http: //pastebin.com/vWnbvu5m

4

1 回答 1

1

You can create artist objects and push() each one you create into an array. Similarly, album and song may be objects push()ed into corresponding arrays attached to their parent objects.

var artists = [];
// for each artist we have
    var artist = {};
    artist.name = 'Music Artist 1';
    artist.albums = [];
    // for each album we have
        var album = {};
        album.title = 'Album Nr1'
        album.songs = [];
        // for each song that we have
            var song = {};
            song.title = 'Track 1';
            album.songs.push(song);
        // end song loop
        artist.albums.push(album);
    // end album loop
    artists.push(artist)
// end artist loop

If you then need this information in JSON format, you can parse it using a JSON parser. Or you can programmatically read data from each artist by looping over the artists array.

// returns name of first artist in array
artists[0].name;

// returns title of first album by first artist in respective arrays
artists[0].albums[0].title;

// returns title of first song in first album by first artist in respective arrays
artists[0].albums[0].songs[0].title;
于 2013-08-29T19:07:27.137 回答