简体   繁体   English

将 .txt 文件中的单词转换为数组?

[英]Convert words from .txt file to an array?

I'm developing a game which needs a wordlist called "words.txt" and inside this file, it looks like :我正在开发一个游戏,它需要一个名为“words.txt”的词表,在这个文件中,它看起来像:

zoologie
zozoter
Zurich
zygote

And i need to convert it to an Array for VueJS which will look like :我需要将其转换为 VueJS 的数组,如下所示:

export default {
  data(){
     return{
      words: ["zoologie", "zozoter", "Zurich", "Zygote"]
     }
  }
}

The problem is that i don't know how to do it, because if i proceed 1by1, it will take a long time (22k words in it)问题是我不知道怎么做,因为如果我按 1by1 进行,需要很长时间(里面有 22k 个字)

Thank you.谢谢你。

If you have node installed, you can do something like this:如果您安装了节点,则可以执行以下操作:

let fs = require("fs");
let string = `export default {
  data(){
     return{
      words: [`;
fs.readFileSync("words.txt").toString().split("\n").forEach(el => {
  string += `"${el}",`;
});
string += `]
     }
  }
}`;
fs.writeFile("yourfile.js", string);

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

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