考虑:
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);