Here's a function that will take n random elements from array
, and return them, based off a fisher-yates shuffle. Note that it will modify the array
argument.
function randomFrom(array, n) {
var at = 0;
var tmp, current, top = array.length;
if(top) while(--top && at++ < n) {
current = Math.floor(Math.random() * (top - 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array.slice(-n);
}
Assuming your code works how I think it does, you already have an array of userids:
var random10 = randomFrom(friendIds, 10);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…