gpt4 book ai didi

javascript - 遍历对象数组并返回随机对象值 - JavaScript

转载 作者:行者123 更新时间:2023-12-05 09:09:43 26 4
gpt4 key购买 nike

我正在尝试遍历对象数组并显示数据中的随机引号,到目前为止我的代码返回 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);
}

我正在尝试随机显示引号(字符串)。

最佳答案

您可能希望通过此方式获取报价:

const randomQuote = quotesToUse[Math.floor(Math.random() * quotesToUse.length)].quote;

我不知道你想用循环做什么。

关于javascript - 遍历对象数组并返回随机对象值 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62142835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com