Answers for "javascript get random items from array"

49

javascript get random array value

//get random value from array
var colors = ["red","blue","green","yellow"];
var randColor = colors[Math.floor(Math.random() * colors.length)];
Posted by: Guest on July-31-2019
16

javascript how to get a random element from an array

var items = ['Yes', 'No', 'Maybe'];
var item = items[Math.floor(Math.random() * items.length)];
Posted by: Guest on March-19-2020
8

pick a random element from an array javascript

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

var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
Posted by: Guest on April-19-2020
0

javascript array with random values

function randomArrayStr(maxWordLength,arrayLength = false){
    const arr = []
    while(arrayLength){
        arr.push(Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, maxWordLength))
        --arrayLength
    }
    return arr
}
Posted by: Guest on August-21-2021
-1

how to get random item from an array

//Function to make it easier
Array.prototype.random = function() {
    return this[Math.floor((Math.random() * this.length))];
};

var colors = ["red","blue","green","yellow"];
var randomColor = colors.random();
Posted by: Guest on February-24-2021

Code answers related to "javascript get random items from array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language