如何在JavaScript中将单词列表转换为数组

时间:2018-08-25 09:27:22

标签: javascript arrays word-list

我是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];  

3 个答案:

答案 0 :(得分:1)

  

将数组内容作为这样的字符串作为变量:

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";
  

然后写

var wordsArray = words.split(' ');
console.log(wordsArray);
  

那是JS的魔力

答案 1 :(得分:0)

欢迎堆栈溢出。

这里是您想要做的一种方式:

Set wb = ThisWorkbook

这省去了必须在所有地方都加引号和逗号的麻烦。

答案 2 :(得分:0)

我要说的第一件事是您的语法不正确。如果您要表达一个字符串,请使用格式-var a = "fox hen lol haha";

如果您将变量words设置为字符串,并且每个单词都用空格分隔,那么我可以解决您的问题。

var words = "bla bla bla your words here";
var arrayOfWords = []; // Create the array to store words
arrayOfWords = words.split(" ");

就完成了。数组arrayOfWords包含字符串中的所有单词作为单独的数组对象。

如果您仍然感到困惑,请在下面发表评论。