简体   繁体   中英

Javascript create array from local words.txt 2021

I have a txt file called test.txt.

Inside of it there are words that are basically laid out like this.

ability
able
about
above
accept
according
account
across
act
action
activity
actually
add
address
administration
admit
adult
affect
after
again
against
age
agency
agent
ago

I want to use these words turn them into an array and store into one variable.

Something like this.

var words = ["ability", "about", "above", "action"...];

I am only using javascript and nothing else how do I do this?

Assuming you are using node.js since you are accessing a file.

const fs = require('fs');
let array = fs.readFile('./test.txt').split(/\r?\n/g);

Otherwise the answer is: JavaScript is executed client-side and won't handle files.

try it:

file.split(/\r\n|\r|\n/g)

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