javascript - JavaScript:如何仅显示一个插入字符串的数组项?
问题描述
大部分代码已从我从该站点更改的主要源代码中更改:NewRetroWave 名称生成器: https ://codepen.io/njmcode/details/JdRaWX
上面的网站及其代码片段利用了 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 arrayOne = ["one", "two", "three", "four", "five"];
const sentenceOne = [`Your generated item is: ${arrayOne}`, `Random Item Generated: ${arrayOne}`, `Generated: ${arrayOne}`];
function randMathFunc(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const initialize = (function(){
function randArray(generateArray) {
return generateArray[randMathFunc(0, generateArray.length - 1)];
}
function generateItem() {
var itm1Gen = randMathFunc(1, 1),
itm1Name = [];
for (n = 0; n < itm1Gen; n++) {
var complete = false;
while (!complete) {
let newItm1Gen = randArray(items);
if (itm1Name.indexOf(newItm1Gen) == -1) {
itm1Name.push(newItm1Gen);
complete = true;
}
}
}
var itemKey = itm1Name.join( " " );
return itemKey;
}
function displayItem(item){
document.getElementById( 'itemkeyname' ).innerHTML = item;
}
function contentRetriever() {
let item = generateItem();
displayItem(item);
}
function runProgram() {
items = sentenceOne;
contentRetriever();
document.getElementById( 'generatebutton' ).onclick = function () {
let item = generateItem();
displayItem( item );
}
}
return {
init: runProgram
}
})();
initialize.init();
html {
margin: auto;
padding-top: 255px;
padding-bottom: 255px;
padding-right: 55px;
padding-left: 55px;
}
h3 {
text-transform: uppercase;
}
head, h1, h3 {
position: static;
text-align: center;
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
body {
background-color: whitesmoke;
}
#itemkeyname {
position: static;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
text-transform: uppercase;
}
#contentGenerate, #generatebutton {
position: static;
text-align: center;
font-family: monospace;
}
#generatebutton {
border-top: 0.5px;
border-bottom: 0.5px;
border-style: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="snippet.css" />
</head>
<body>
<div id="wrapped">
<div id="insideItems">
<div id="itemkeyname"></div>
</div>
</div>
<div id="itemArrayGenerate">
<button type="button"
id="generatebutton"
value="generate">Generate</button>
</div>
<script src="snippet.js"></script>
</body>
</html>
因为,我的问题似乎含糊不清,请告诉我您希望我上传的内容。
这是输出: 编写提示生成器输出
这是我的写作提示生成器的输出,您可以看到它输出整个数组而不是所述数组中的一项。
解决方案
我不知道我是否完全理解您的需要,但是,如果您需要从数组中获取随机项,那就是这样。
var arr = ['one', 'two', 'three', 'four', 'five'];
function randomItemFromArray(arr){
return arr[Math.floor(Math.random() * arr.length)]
}
推荐阅读
- excel - 是否有可以更改复制单元格内容的 vba 代码?
- database - 使用内存 SQLite 进行并发读取的最佳方法?
- python-3.x - 尽管 post() 登录成功,但 Python 请求身份验证失败并使用 get()
- json - 防止 Plug.ErrorHandler 在回调后重新引发错误
- javascript - typescript express 在 nodeJS 通过所需的 require('./main.ts') 运行时无法导出并显示未定义
- android - 如何使用 HILT android 在服务中调用 api
- ios - 如何在 iOS 14 的内联样式日期选择器顶部设置时间选择器?
- java - 仅使用 Mockito 模拟新对象
- javascript - 根据其他 div 的可见性和不可见性调整 div 的大小(仅在 javascript 中)
- c - 如何从共享对象(.so)执行功能