Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #random #item #array #javascript
ADD COMMENT
Topic
Name
1+6 =