我正在尝试遍历对象数组并显示数据中的随机引用,到目前为止我的代码返回undefined。知道为什么吗?
到目前为止的代码..
const quotesToUse = [{
quote: "This was the biggest quote ever - Array Object",
author: "Derek Bullshido",
},
{
quote: "Hey hows it going - Array Object",
author: "Paul Frank",
},
{
quote: "Why can I say that - Array Object",
author: "Derek Paul",
},
]
function randomQuotes() {
for (var i = 0; i < 1; i += 1 ) {
const randomQuote = quotesToUse[i][Math.floor(Math.random() * quotesToUse[i].quote.length)];
document.getElementById("container").appendChild(text).append(randomQuote);
}
Run Code Online (Sandbox Code Playgroud)
我试图随机显示报价(字符串)。
您可能希望通过以下方式获得报价:
const randomQuote = quotesToUse[Math.floor(Math.random() * quotesToUse.length)].quote;
Run Code Online (Sandbox Code Playgroud)
我不知道你想用循环做什么。
| 归档时间: |
|
| 查看次数: |
94 次 |
| 最近记录: |