如何在 JavaScript 中将单词列表转换为数组
How to convert word list to array in JavaScript
我是 JavaScript 的新手。
出于学习目的,我正在尝试创建一个简单的文字输入游戏。
我只是停留在制作数组的第一步上。
那么如何将单词列表转换为数组?
var words = [greet
campaign
coffee
care
revise
ridge
pilot
full
prison
wrestle
account
dictionary
start
giant
fast
monarch
patrol
bear
motif
detective
trouser
title
act
speaker
pursuit
penny
appear
ballet
agreement
welcome
similar
sensation
strong
fog
limited
implicit
nursery
neighborhood
infinite
refund
double
achievement
ample
socialist
intensify
nomination
visit
drag
retiree
mislead
situation
credit
oil
clarify
conscience
war
sow
suitcase
fixture
worth
fuss
remain
moment
frighten
spider
breathe
install
interactive
allow
deadly
cabin
restless
service
rise
integrity
artist
short circuit
perception
listen
arrangement
patience
creep
landscape
stain
citizen
microphone
nose
bullet
view
category
reign
horizon
news
social
reliable
passion
stroll
constitutional
adult
city];
Take the array content as string like this in a variable:
var words = "greet campaign coffee care revise ridge pilot full prison wrestle account dictionary start giant fast monarch patrol bear motif detective trouser title act speaker pursuit penny appear ballet agreement welcome similar sensation strong fog limited implicit nursery neighborhood infinite refund double achievement ample socialist intensify nomination visit drag retiree mislead situation credit oil clarify conscience war sow suitcase fixture worth fuss remain moment frighten spider breathe install interactive allow deadly cabin restless service rise integrity artist short circuit perception listen arrangement patience creep landscape stain citizen microphone nose bullet view category reign horizon news social reliable passion stroll constitutional adult city";
and then, write
var wordsArray = words.split(' ');
console.log(wordsArray);
That's magic of JS
欢迎来到 Stack Overflow。
这里有一种方法可以做你想做的事:
var words = "greet campaign coffee care revise ridge pilot full prison wrestle account dictionary start giant fast monarch patrol bear motif detective trouser title act speaker pursuit penny appear ballet agreement welcome similar sensation strong fog limited implicit nursery neighborhood infinite refund double achievement ample socialist intensify nomination visit drag retiree mislead situation credit oil clarify conscience war sow suitcase fixture worth fuss remain moment frighten spider breathe install interactive allow deadly cabin restless service rise integrity artist short circuit perception listen arrangement patience creep landscape stain citizen microphone nose bullet view category reign horizon news social reliable passion stroll constitutional adult city".split(/\s+/)
这可以让您省去编辑时不得不在各处加上引号和逗号的麻烦
首先我想说的是你的语法不正确。如果你试图表达一个字符串,请使用格式 - var a = "fox hen lol haha";
如果你的意思是你的变量 words
是一个字符串,并且每个单词由 space 分隔,我有解决你问题的方法。
var words = "bla bla bla your words here";
var arrayOfWords = []; // Create the array to store words
arrayOfWords = words.split(" ");
完成了。数组 arrayOfWords
包含字符串中的所有单词作为单独的数组对象。
如果还有不明白的,欢迎在下方留言。
我是 JavaScript 的新手。
出于学习目的,我正在尝试创建一个简单的文字输入游戏。
我只是停留在制作数组的第一步上。
那么如何将单词列表转换为数组?
var words = [greet
campaign
coffee
care
revise
ridge
pilot
full
prison
wrestle
account
dictionary
start
giant
fast
monarch
patrol
bear
motif
detective
trouser
title
act
speaker
pursuit
penny
appear
ballet
agreement
welcome
similar
sensation
strong
fog
limited
implicit
nursery
neighborhood
infinite
refund
double
achievement
ample
socialist
intensify
nomination
visit
drag
retiree
mislead
situation
credit
oil
clarify
conscience
war
sow
suitcase
fixture
worth
fuss
remain
moment
frighten
spider
breathe
install
interactive
allow
deadly
cabin
restless
service
rise
integrity
artist
short circuit
perception
listen
arrangement
patience
creep
landscape
stain
citizen
microphone
nose
bullet
view
category
reign
horizon
news
social
reliable
passion
stroll
constitutional
adult
city];
Take the array content as string like this in a variable:
var words = "greet campaign coffee care revise ridge pilot full prison wrestle account dictionary start giant fast monarch patrol bear motif detective trouser title act speaker pursuit penny appear ballet agreement welcome similar sensation strong fog limited implicit nursery neighborhood infinite refund double achievement ample socialist intensify nomination visit drag retiree mislead situation credit oil clarify conscience war sow suitcase fixture worth fuss remain moment frighten spider breathe install interactive allow deadly cabin restless service rise integrity artist short circuit perception listen arrangement patience creep landscape stain citizen microphone nose bullet view category reign horizon news social reliable passion stroll constitutional adult city";
and then, write
var wordsArray = words.split(' ');
console.log(wordsArray);
That's magic of JS
欢迎来到 Stack Overflow。
这里有一种方法可以做你想做的事:
var words = "greet campaign coffee care revise ridge pilot full prison wrestle account dictionary start giant fast monarch patrol bear motif detective trouser title act speaker pursuit penny appear ballet agreement welcome similar sensation strong fog limited implicit nursery neighborhood infinite refund double achievement ample socialist intensify nomination visit drag retiree mislead situation credit oil clarify conscience war sow suitcase fixture worth fuss remain moment frighten spider breathe install interactive allow deadly cabin restless service rise integrity artist short circuit perception listen arrangement patience creep landscape stain citizen microphone nose bullet view category reign horizon news social reliable passion stroll constitutional adult city".split(/\s+/)
这可以让您省去编辑时不得不在各处加上引号和逗号的麻烦
首先我想说的是你的语法不正确。如果你试图表达一个字符串,请使用格式 - var a = "fox hen lol haha";
如果你的意思是你的变量 words
是一个字符串,并且每个单词由 space 分隔,我有解决你问题的方法。
var words = "bla bla bla your words here";
var arrayOfWords = []; // Create the array to store words
arrayOfWords = words.split(" ");
完成了。数组 arrayOfWords
包含字符串中的所有单词作为单独的数组对象。
如果还有不明白的,欢迎在下方留言。