Answers for "get random index from array js"

9

how to get a random element of an array javascript

var foodItems = ["Bannana", "Apple", "Orange"];
var theFood = foodItems[Math.floor(Math.random() * foodItems.length)];
/* Will pick a random number from the length of the array and will go to the
corosponding number in the array E.G: 0 = Bannana */
Posted by: Guest on May-30-2020
0

get random element from array js

var item = items[Math.floor(Math.random() * items.length)];
Posted by: Guest on October-28-2020
0

random item from array javascript

function RandomItemFromArray(myArray) {
    return myArray[Math.floor(Math.random() * myArray.length)]
}

var fruit = [ "Apples", "Bananas", "Pears", ];

let fruitSample = RandomItemFromArray(fruit)
Posted by: Guest on February-14-2021
1

get random elements from array javascript

array.sort(() => Math.random() - Math.random()).slice(0, n)
Posted by: Guest on September-10-2020
-1

Get a random value from an array in JS

const randomElement = array[Math.floor(Math.random() * array.length)];
Posted by: Guest on June-22-2021

Code answers related to "get random index from array js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language