简体   繁体   中英

How do I use a .txt file and read each line as an array?

I'm trying to create a web app where I have some sort of a flash card, but the information of the flash card will be coming from a .txt file I created.

I'd like to be able to read each line as it's own question or array, and then have the answer for that question in another .txt file that will match the same line so I can use the same index value.

I'm sorry if it sounds confusing. I am just getting started on the idea.

Thank you.

Use one of the examples in MDN - Using files from web applications to get the file you want to process. Once you have selected your file, you can use a FileReader.readAsText() to get the full text of the file as a string.

let myArray;

let fileReader = new FileReader();

fileReader.onload = (event) => {
   myArray = event.target.result.split('\n');
}

fileReader.readAsText(myFile);

Take a deep read on the first article. It has lots of examples of working with files. It has helped me a lot

PS: if it worked, please, mark this as the solution. I need reputation to be able to comment ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM