_.shuffle(list)
返回一个随机乱序的list副本数组, 使用 Fisher-Yates shuffle 来进行随机乱序.
//数组
var arr = [1, 2, 3, 4, 5, 6];
console.log(_.shuffle(arr)); //may be => [2, 4, 5, 1, 6, 3]
//字符串
console.log(_.shuffle('123456')); //may be => ["5", "1", "2", "6", "4", "3"]
//对象返回的也是数组。
var obj = {a:1, b:2, c:3, d:4, e:5};
console.log(_.shuffle(obj)); //may be => [1, 3, 4, 2, 5]
//arguments
(function(){
console.log(_.shuffle(arguments)); //may be => [1, 3, 4, 5, 2]
}(1, 2, 3, 4, 5))
var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = _.shuffle(arr1);
console.log(arr1); // [1, 2, 3, 4, 5, 6]
console.log(arr2); //may be => [2, 4, 6, 1, 5, 3]
console.log(_.shuffle([])); //=> []
console.log(_.shuffle(1)); //=> []
console.log(_.shuffle(null)); //=> []
console.log(_.shuffle(undefined)); //=> []
console.log(_.shuffle(NaN)); //=> []
function shuffle(list){
return _.sample(list, list.length);
}
var list = [1, 2, 3, 4, 5, 6];
var arr1 = _.shuffle(list);
var arr2 = shuffle(list);
console.log(arr1); //may be => [1, 4, 5, 2, 6, 3]
console.log(arr2); //may be => [6, 5, 1, 2, 3, 4]