教程 lodash tutorial

_.shuffle(collection)

_.shuffle(collection) 用于随机打乱集合中的元素,并返回一个新的打乱后的数组。

  • collection:要打乱顺序的集合,可以是数组、对象或类数组对象。

应用举例:

javascript
// 原始集合
const collection = [1, 2, 3, 4, 5];

// 打乱集合中元素的顺序
const shuffledCollection = _.shuffle(collection);

console.log(shuffledCollection);
// 输出:打乱顺序后的新数组

在这个例子中,_.shuffle 方法打乱了原始集合中元素的顺序,并返回一个新的打乱后的数组。