node js how to turn random array element code example
Example 1: how to get a random element of an array javascript
var foodItems = ["Bannana", "Apple", "Orange"];
var theFood = foodItems[Math.floor(Math.random() * foodItems.length)];
Example 2: js get random from array
Array.prototype.random = function() {
return this[Math.floor(Math.random() * this.length)];
}
var result = ["Hello", "world"].random();
Example 3: get random elements from array javascript
const arr = myArray
.map((a) => ({sort: Math.random(), value: a}))
.sort((a, b) => a.sort - b.sort)
.map((a) => a.value)