JavaScript:如何仅显示插入到字符串中的一个数组项?

JavaScript:如何仅显示插入到字符串中的一个数组项?,javascript,Javascript,大部分代码已从我在此站点更改的主要源代码更改:NewRetroWave名称生成器: 上面的站点及其代码片段利用jQuery库。至于我的项目,我只使用JavaScript、HTML和CSS const arrayOne = [one, two, three, four, five,]; 上面的代码是将所有项目输出到下面代码中的数组 其中,我只想在通过HTML按钮函数生成代码时显示一项 const sentenceOne = [`Your generated item is: ${arrayOn

大部分代码已从我在此站点更改的主要源代码更改:NewRetroWave名称生成器:

上面的站点及其代码片段利用jQuery库。至于我的项目,我只使用JavaScript、HTML和CSS

const arrayOne = [one, two, three, four, five,];
上面的代码是将所有项目输出到下面代码中的数组 其中,我只想在通过HTML按钮函数生成代码时显示一项

const sentenceOne = [`Your generated item is: ${arrayOne}`, `Random Item Generated: ${arrayOne}`, `Generated: ${arrayOne}`];
常量数组=[“一”、“二”、“三”、“四”、“五”]; const-sentenceOne=[`您生成的项目是:${arrayOne}`,`随机生成的项目:${arrayOne}`,`生成的:${arrayOne}`]; 函数randMathFunc(最小值,最大值){ 返回Math.floor(Math.random()*(max-min+1)+min); } 常量初始化=(函数(){ 函数randArray(GeneratorArray){ 返回generatarray[randMathFunc(0,generatarray.length-1)]; } 函数generateItem(){ var itm1Gen=randMathFunc(1,1), itm1Name=[]; 对于(n=0;n
html{
保证金:自动;
垫面:255px;
垫底:255px;
右边填充:55px;
左侧填充:55px;
}
h3{
文本转换:大写;
}
头部,h1,h3{
位置:静态;
文本对齐:居中;
字体系列:“Trebuchet MS”、“Lucida Sans Unicode”、“Lucida Grande”、“Lucida Sans”、Arial、Sans serif;
}
身体{
背景色:白烟;
}
#itemkeyname{
位置:静态;
文本对齐:居中;
字体系列:Arial、Helvetica、无衬线字体;
文本转换:大写;
}
#contentGenerate,#generatebutton{
位置:静态;
文本对齐:居中;
字体系列:monospace;
}
#生成按钮{
边框顶部:0.5px;
边框底部:0.5px;
边框样式:无;
}

页面标题
生成

我不知道我是否完全理解您需要什么,但是,如果您需要从数组中获取随机项,那就是方法

var arr = ['one', 'two', 'three', 'four', 'five'];

function randomItemFromArray(arr){
   return arr[Math.floor(Math.random() * arr.length)]
}

您当前仅在您的
sentenceOne
数组中使用
arrayOne
,即随机词数组:

const sentenceOne = [`Your generated item is: ${arrayOne}`, `Random Item Generated: ${arrayOne}`, `Generated: ${arrayOne}`];
但是你已经把
数组硬编码到每个
句子中了<代码>第一句
现在正是:

[
  "Your generated item is: one,two,three,four,five",
  "Random Item Generated: one,two,three,four,five",
  "Generated: one,two,three,four,five"
]
生成随机字符串时,您需要从
数组中选择随机前缀字符串(
生成的…
)和随机项。我建议定义
sentenceOne
句子,只包含前缀字符串,然后在生成新项时,在
arrayOne
sentenceOne
上调用
randArray

let newItm1Gen = randArray(items) + randArray(arrayOne);
常量数组=[“一”、“二”、“三”、“四”、“五”]; const sentenceOne=[`您生成的项为:`,`随机项生成:`,`生成:`]; 函数randMathFunc(最小值,最大值){ 返回Math.floor(Math.random()*(max-min+1)+min); } 常量初始化=(函数(){ 函数randArray(GeneratorArray){ 返回generatarray[randMathFunc(0,generatarray.length-1)]; } 函数generateItem(){ var itm1Gen=randMathFunc(1,1), itm1Name=[]; 对于(n=0;n
html{
保证金:自动;
垫面:255px;
垫底:255px;
右边填充:55px;
左侧填充:55px;
}
h3{
文本转换:大写;
}
头,,
h1,
h3{
位置:静态;
文本对齐:居中;
字体系列:“Trebuchet MS”、“Lucida Sans Unicode”、“Lucida Grande”、“Lucida Sans”、Arial、Sans serif;
}
身体{
背景色:白烟;
}
#itemkeyname{
位置:静态;
文本对齐:居中;
字体系列:Arial、Helvetica、无衬线字体;
文本转换:大写;
}
#内容生成,
#生成按钮{
位置:静态;
文本对齐:居中;
字体系列:monospace;
}
#生成按钮{
边框顶部:0.5px;
边框底部:0.5px;
边框样式:无;
}

页面标题
生成

此代码已应用于提出的问题中。我只是想弄清楚如何一次只显示数组中随机生成的一个项