📜  Lodash_.shuffle() 方法

📅  最后修改于: 2022-05-13 01:56:52.441000             🧑  作者: Mango

Lodash_.shuffle() 方法

_.shuffle()方法通过返回新数组来打乱集合。

句法:

_.shuffle(collection)

参数:此方法接受如上所述和如下所述的单个参数:

  • 集合:此参数保存要检查的集合。

返回值:该方法返回洗牌后的新数组。

示例一:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。

Javascript
// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.shuffle() method
var gfg = _.shuffle([1, 2, 3,4]);
  
// Printing the output 
console.log(gfg);


Javascript
// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.shuffle() method
var gfg = _.shuffle(["g", "e", "e", "k", "s", "f", "o",
                     "r", "g", "e", "e", "k", "s"]);
  
// Printing the output 
console.log(gfg);


输出 :

[4, 1, 3, 2]

示例 2:

Javascript

// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.shuffle() method
var gfg = _.shuffle(["g", "e", "e", "k", "s", "f", "o",
                     "r", "g", "e", "e", "k", "s"]);
  
// Printing the output 
console.log(gfg);

输出 :

["o", "e", "k", "s", "e", "e", "s",
 "g", "r", "e", "g", "f", "k"]

注意:此代码在普通 JavaScript 中不起作用,因为它需要安装库 lodash。