gpt4 book ai didi

javascript - 从JavaScript数组获取随机值

转载 作者:行者123 更新时间:2023-11-28 06:39:07 25 4
gpt4 key购买 nike

考虑:

var myArray = ['January', 'February', 'March'];    


如何使用JavaScript从此数组中选择随机值?

最佳答案

这是一个简单的单线

const randomElement = array[Math.floor(Math.random() * array.length)];






const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const randomMonth = months[Math.floor(Math.random() * months.length)];

console.log("random month =>", randomMonth);

关于javascript - 从JavaScript数组获取随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34005509/

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