简体   繁体   English

如何使用 .txt 文件并将每一行作为数组读取?

[英]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.我正在尝试创建一个网络应用程序,其中我有某种闪存卡,但闪存卡的信息将来自我创建的 .txt 文件。

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.我希望能够读取每一行,因为它是自己的问题或数组,然后在另一个匹配同一行的 .txt 文件中获得该问题的答案,以便我可以使用相同的索引值。

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.使用MDN中的示例之一- 使用来自 Web 应用程序的文件来获取要处理的文件。 Once you have selected your file, you can use a FileReader.readAsText() to get the full text of the file as a string.选择文件后,您可以使用FileReader.readAsText()以字符串形式获取文件的全文。

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. PS:如果有效,请将此标记为解决方案。 I need reputation to be able to comment ^_^我需要声誉才能发表评论^_^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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