get random value from array js code example
Example 1: javascript get random array value
var colors = ["red","blue","green","yellow"];
var randColor = colors[Math.floor(Math.random() * colors.length)];
Example 2: 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 3: get random item from array javascript
const randomElement = array[Math.floor(Math.random() * array.length)];
Example 4: get random entry from array javascript
const rnd = (arr) => { return arr[Math.floor(Math.random() * arr.length)] };
Example 5: get random elements from array javascript
array.sort(() => Math.random() - Math.random()).slice(0, n)
Example 6: 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)