Answers for "js get random n items from array"

3

get random entry from array javascript

const rnd = (arr) => { return arr[Math.floor(Math.random() * arr.length)] };
Posted by: Guest on January-12-2021
0

javascript get n random elements from array

// Shuffle array
const shuffled = array.sort(() => 0.5 - Math.random());

// Get sub-array of first n elements after shuffled
let selected = shuffled.slice(0, n);
Posted by: Guest on February-07-2021

Code answers related to "js get random n items from array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language