Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a timetable for the upcoming rock concerts. It consists of bands, towns, dates and venues,
- separated by '|'. Your task is to write a JavaScript function that prints at the console a JSON string that holds
- the towns, venues for each town and list of bands for each venue (see the example below). */
- "use strict";
- function printToJSONstring(arr) {
- var i;
- var concertMap = {};
- for (i = 0; i < arr.length; i += 1) {
- var lineArray = arr[i].split('|').filter(Boolean);
- var town = lineArray[1].trim();
- var stadium = lineArray[3].trim();
- var band = lineArray[0].trim();
- if (!(town in concertMap)) {
- concertMap[town] = {};
- }
- if (!(stadium in concertMap[town])) {
- concertMap[town][stadium] = [];
- }
- if (concertMap[town][stadium].indexOf(band) == -1) {
- concertMap[town][stadium].push(band);
- }
- }
- var townKeys = Object.keys(concertMap).sort();
- var outputMap = {};
- for (i = 0; i < townKeys.length; i += 1) {
- outputMap[townKeys[i]] = {};
- if (concertMap.hasOwnProperty(townKeys[i])) {
- var stadiumKeys = Object.keys(concertMap[townKeys[i]]).sort();
- for (var j = 0; j < stadiumKeys.length; j += 1) {
- if (concertMap[townKeys[i]].hasOwnProperty(stadiumKeys[j])) {
- outputMap[townKeys[i]][stadiumKeys[j]] = concertMap[townKeys[i]][stadiumKeys[j]].sort();
- }
- }
- }
- }
- console.log(JSON.stringify(outputMap));
- }
- printToJSONstring([
- 'ZZ Top | London | 2-Aug-2014 | Wembley Stadium',
- 'Iron Maiden | London | 28-Jul-2014 | Wembley Stadium',
- 'Metallica | Sofia | 11-Aug-2014 | Lokomotiv Stadium',
- 'Helloween | Sofia | 1-Nov-2014 | Vassil Levski Stadium',
- 'Iron Maiden | Sofia | 20-June-2015 | Vassil Levski Stadium',
- 'Helloween | Sofia | 30-July-2015 | Vassil Levski Stadium',
- 'Iron Maiden | Sofia | 26-Sep-2014 | Lokomotiv Stadium',
- 'Helloween | London | 28-Jul-2014 | Wembley Stadium',
- 'Twisted Sister | London | 30-Sep-2014 | Wembley Stadium',
- 'Metallica | London | 03-Oct-2014 | Olympic Stadium',
- 'Iron Maiden | Sofia | 11-Apr-2016 | Lokomotiv Stadium',
- 'Iron Maiden | Buenos Aires | 03-Mar-2014 | River Plate Stadium'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement