Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript pick multiple random from array

const list = [1, 2, 3, 4, 5, 6];

// shuffle your list with the sort function:
const shuffledList = list.sort(() => Math.random() - 0.5);
// generate a size for the new list
const newListSize = Math.floor(Math.random() * list.length)
// pick the first "newListSize" elements from "shuffledList"
const newList = shuffledList.slice(0, newListSize)

console.log(newList);
// [3, 2, 6]; [5]; [4, 1, 2, 6, 3, 5]; []; etc..
Comment

how to get more than 1 random item in an array javascript

var getMeRandomElements = function(sourceArray, neededElements) {
    var result = [];
    for (var i = 0; i < neededElements; i++) {
        result.push(sourceArray[Math.floor(Math.random()*sourceArray.length)]);
    }
    return result;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js today timestamp 
Javascript :: tab active check 
Javascript :: jquery search for string in text 
Javascript :: wait for element to load 
Javascript :: javascript from method 
Javascript :: remove axis tick ends d3 
Javascript :: transitionduration 
Javascript :: ajax post body parameters 
Javascript :: jquery get URL slug 
Javascript :: Vuejs v-model when enter pressed 
Javascript :: content editable vuejs 
Javascript :: json rename key 
Javascript :: javascript beforeunload 
Javascript :: query selector has clas 
Javascript :: neo4j create relationship between existing nodes 
Javascript :: react native get mac address 
Javascript :: jquery remove option from dropdown 
Javascript :: Capitalise a String 
Javascript :: slicknav cdn 
Javascript :: how to change text of div in javascript 
Javascript :: Get the Status Code of a Fetch HTTP Response 
Javascript :: fluttter http get 
Javascript :: moment date without timezone 
Javascript :: discord.js listen for message 
Javascript :: socket.io reconnect example 
Javascript :: jquery get request with headers 
Javascript :: how to remove an element from a parent element javascript 
Javascript :: angular decode url 
Javascript :: axios get status code 
Javascript :: jquery detect change in textarea content 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =