/**
* create map of shows, seasons, episodes
* @param {data}
* @returns {map}
*/
function ShowGuide(data){
let showMap = new Map();
let seasonMap;
let season;
let episodes;
for (let i=0; i < data.length; i++){
//if show exists retrieve it to insert more
if (showMap.has(data[i].showid)){
let show = showMap.get(data[i].showid);
//if show and season already exist, insert episodes in map
if (show.season.has(data[i].seasonnumber)){
episodes = show.season.get(data[i].seasonnumber);
episode = {
'episodenumber': data[i].episodenumber,
'episodetitle': data[i].episodetitle
};
episodes.push(episode);
} else {
//season does not exist, insert season and episode in map
episodes = [{
'episodenumber': data[i].episodenumber,
'episodetitle': data[i].episodetitle
}];
show.season = seasonMap.set(data[i].seasonnumber, episodes);
}
} else {
//show doesn't exist, create it with season and episode
seasonMap = new Map();
episodes = [{
'episodenumber': data[i].episodenumber,
'episodetitle': data[i].episodetitle
}];
seasonMap.set(data[i].seasonnumber, episodes);
showMap.set(data[i].showid, {'showtitle': data[i].showtitle, 'season': seasonMap});
}
}
return showMap;
}
//data used in example
const data = [
{"showid": 1234, "showtitle": "Game of Thrones", "seasonnumber": 1, "episodenumber": 1, "episodetitle": "Winter Is Coming"},
{"showid": 1234, "showtitle": "Game of Thrones", "seasonnumber": 1, "episodenumber": 2, "episodetitle": "The Kingsroad"},
{"showid": 1234, "showtitle": "Game of Thrones", "seasonnumber": 1, "episodenumber": 3, "episodetitle": "Lord Snow"},
{"showid": 1234, "showtitle": "Game of Thrones", "seasonnumber": 2, "episodenumber": 1, "episodetitle": "The North Remembers"},
{"showid": 1234, "showtitle": "Game of Thrones", "seasonnumber": 2, "episodenumber": 2, "episodetitle": "The Night Lands"},
{"showid": 1234, "showtitle": "Game of Thrones", "seasonnumber": 2, "episodenumber": 3, "episodetitle": "What Is Dead May Never Die"},
{"showid": 2345, "showtitle": "Stranger Things", "seasonnumber": 1, "episodenumber": 1, "episodetitle": "The Vanishing of Will Byers"},
{"showid": 2345, "showtitle": "Stranger Things", "seasonnumber": 1, "episodenumber": 2, "episodetitle": "The Weirdo on Maple Street"},
{"showid": 2345, "showtitle": "Stranger Things", "seasonnumber": 1, "episodenumber": 3, "episodetitle": "Holly, Jolly"},
{"showid": 2345, "showtitle": "Stranger Things", "seasonnumber": 2, "episodenumber": 1, "episodetitle": "Madmax"},
{"showid": 2345, "showtitle": "Stranger Things", "seasonnumber": 2, "episodenumber": 2, "episodetitle": "The Boy Who Came Back to Life" }
];
myShowGuide = new ShowGuide(data);
let gotShows = myShowGuide.get(1234);
let gotSeasonOne = gotShows.season.get(1);
let gotSeasonTwo = gotShows.season.get(2);
let stShows = myShowGuide.get(2345);
let stSeasonOne = stShows.season.get(1);
let stSeasonTwo = stShows.season.get(2);
let mapKeys = myShowGuide.keys();
let mapValues = myShowGuide.values();