Just two lines :
// 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);
DEMO:
More Related Contents:
- Parsing JSON objects for HTML table
- Convert html table to array in javascript
- Get one element title and add this to another div in jquery
- how do i submit message to my email by html or js?
- Get the longest text in HTML document using Jquery or javascript
- Hide/Show image on one click [closed]
- How to create button to check uncheckable checkbox
- How to move an element into another element?
- How to perform a real time search and filter on a HTML table
- Jquery Event won’t fire after ajax call
- How to bind Events on Ajax loaded Content?
- Get unique selector of element in Jquery
- How to get all selected values from ?
- Set content of iframe
- Retrieving file names out of a multi-file upload control with javascript
- Save a pre element as PDF with CSS
- How To Show And Hide Input Fields Based On Radio Button Selection
- jQuery Validation using the class instead of the name value
- Center flex item in container, when surrounded by other flex items
- Onclick reload the div only
- jQuery: How to listen for DOM changes?
- Sorting an Array of JavaScript Objects a Specific Order (using existing function)
- submit event does not fire if submit initiated programmatically
- How can I remove wrapper (parent element) without removing the child?
- javaScript: Can a comma occur after the last set of values in an array?
- How to use pagination on HTML tables?
- How to access jQuery in HTML 5 web worker
- Incrementing value continuously on mouse hold
- Content of html page changed by jQuery but “View Source” don’t reflect the changes
- How can I reorder elements with JavaScript?