Map2
Datagrunnlaget er en CSV-fil med en linje for hver finale.
Du kan se den i nettleseren her: data.txt.
De første linjene i fila:
// year:winner:looser 1956:Real Madrid : Reims 1957:Real Madrid : Fiorentina 1958:Real Madrid : Milan ...
Test
Koden
Lasting av csv-fila, data.csv
_loadCSV()
// load file:data.csv, clean it and make // an array of matches as strings: // 'winner:year:loooser' function loadCSV(){ dump.innerHTML=""; matches=[]; fetch("data.txt", {cache: "no-cache"}) .then(function(response) { if(response.ok) return response.text(); throw(response.statusText); }) .then(function(myTxt) { var lines=myTxt.split('\n'); for(var i=0;i<lines.length;i++){ if((lines[i].indexOf('/')==-1) &&(lines[i].length >2)){ var parts=lines[i].split(':'); var y=parts[0].trim(); var w=parts[1].trim(); var l=parts[2].trim(); matches.push(w+':'+y+':'+l); } } // sort on winner matches.sort(); for(var i=0;i< lines.length;i++) dump.innerHTML+=matches[i]+"\n"; }) .catch(function(err) { alert(" data er ikke lastet \n"+err); }); }
Bygging av json-struktur
_buildJson()
// convert the string array 'matches' to a suitable // json format that will work as a Map function buildJson(){ dump.innerHTML=""; if(!matches){ dump.innerHTML="loadCSV first"; return; } var temp=new Map(); //console.log(matches.length); for(var i=0; i< matches.length; i++){ var parts=matches[i].split(":"); var W=parts[0]; var Y=parts[1]; var L=parts[2]; //console.log(W,Y,L); if(!temp.has(W)) temp.set(W,{"finaler":[{"aar":Y,"vs":L}]}); else temp.get(W).finaler.push({"aar":Y,"vs":L}); } var T='['; var tIx=0; temp.forEach(function(value,key){ if(tIx>0) T+='\n'; T+='\n["'+key+'",\n'; T+=JSON.stringify(value)+'],'; tIx++; }); T=T.slice(0, -1); T+='\n]'; teamtxt=T; dump.innerHTML=teamtxt; }
Save json
_saveJson()
// send a txt to python and save it as:teams.json function saveJson(){ dump.innerHTML=""; if(!teamtxt){ dump.innerHTML="build Json first"; return; } console.clear(); var pythonAdr="storeFile.py"; var formData=new FormData(); formData.append('content',teamtxt); formData.append('filename','teams.json'); fetch(pythonAdr, { method: 'PUT', body:formData }) .then(function(response){ return response.text(); }) .then(function(myText) { console.log(myText); dump.innerHTML=myText; }) .catch(function(err) { dump.innerHTML= "-error-: fila,teams.json, er ikke lagret \n"+err; }); }
Load json
_loadJson()
function loadJson(){ dump.innerHTML=""; fetch("teams.json", {cache: "no-cache"}) .then(function(response) { if(response.ok) return response.json(); throw(response.statusText); }) .then(function(myjson) { teams =new Map(myjson); var T=""; teams.forEach(function(value,key){ T+=key+":"+JSON.stringify(value)+"\n"; }); dump.innerHTML=T; }) .catch(function(err) { dump.innerHTML= "fila,teams.json, er ikke lastet \n"+err; }); }