Use:
var a = [[1,2,3],[4,5,6],[7,8,9]];
console.log(random(a, 4));
displays:
[4, 5, 6, 1]
window.random = function(rows, nbToReturn) { | |
// group all sub arrays in a flat array | |
return [].concat.apply([], rows) | |
// randomize | |
.sort(function(){return 0.5 - Math.random()}) | |
// takes the nbToReturn first | |
.splice(0, nbToReturn); | |
}; |