This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
.txt einlesen und in Liste schreiben
#1

Hallo zusammen! 


Ich übe etwas mit javascript. Für das kleine Programm, an dem ich arbeite, sollte eine .txt-File eingelesen werden und in einer Liste abgespeichert werden. 
Hier mein Versuch: 


class Champion {
    constructor(champName, currentStatus) {
      this.name = champName;
      this.status = currentStatus;
    }
  }

  change();

  function change(){
    var rawFile = new XMLHttpRequest();
      rawFile.open("GET", "test.txt", true);
      rawFile.onreadystatechange = function() {
        if (rawFile.readyState === 4) {
          var allText = rawFile.responseText;
          document.getElementById("test").innerHTML = allText;
          console.log(allText);
          const champArray = textfile.split('\n');
 
        var list = [];
 
        for (let i = 0; i < champArray.length; i++){
            let currentChamp = champArray[i].split(', ');
            const c = new Champion;
            c.champName = currentChamp[0];
            c.currentStatus = currentChamp[1];
            list.push(c);
            console.log(list);
            return allText;
        }
    }
      //console.log(allText);
      rawFile.send();
  }
};
Weiß jemand, wie ich das am besten mache?
Danke im Voraus!  Smile

(Die Datei ist folgendermaßen aufgebaut:
Name1, 1
Name2, 1
Name3, 0
Name4, 1
Zitieren


Nachrichten in diesem Thema
.txt einlesen und in Liste schreiben - von prodliluk - 05.10.2022, 02:05

Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste