Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

pick n random items from array javascript

// 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);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #pick #random #items #array #javascript
ADD COMMENT
Topic
Name
2+5 =