How to randomly extract an element or several elements from an array.
If the array is
var items = ['1','2','4','5','6','7','8','9','10'];
1. Randomly extract an element from the array items
var item = items[Math.floor(Math.random()*items.length)];
2. Randomly take several elements from a random array in the previous article
function getRandomArrayElements(arr, count) {var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;while (i-- > min) {index = Math.floor((i + 1) * Math.random());temp = shuffled[index];shuffled[index] = shuffled[i];shuffled[i] = temp;}return shuffled.slice(min);}var items = ['1','2','4','5','6','7','8','9','10'];console.log( getRandomArrayElements(items, 4) );The above is the method of randomly extracting several array elements from JS introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!