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;