一尘不染

从JavaScript数组获取随机值

javascript

考虑:

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

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


阅读 310

收藏
2020-04-22

共1个答案

一尘不染

这是一个简单的单线

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);
2020-04-22