考虑:
var myArray = ['January', 'February', 'March'];
如何使用JavaScript从这个数组中选择一个随机值?
发布于 2018-08-16 15:45:23
将原型函数添加到Array类甚至更简单:
Array.prototype.randomElement = function () {
return this[Math.floor(Math.random() * this.length)]
}
只需键入以下命令,就可以得到一个随机数组元素:
var myRandomElement = myArray.randomElement()
for (var prop in myArray) {
if (myArray.hasOwnProperty(prop)) {
...
}
}
发布于 2018-08-16 16:16:55
var rand = myArray[Math.floor(Math.random() * myArray.length)];
https://stackoverflow.com/questions/-100002215
复制相似问题