Skip to content
Related Articles

Related Articles

Improve Article

D3.js shuffle() method

  • Last Updated : 14 Sep, 2020

With the help of d3.shuffle() method, we can get the randomly shuffled array of values from the source array by using Fisher–Yates shuffle algorithm and return a single array.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

d3.shuffle(array[, start[, stop]])

Return value: It returns a single array having values from the source array.

Note: To execute the below examples you have to install the d3 library by using the command prompt for the following command.



npm install d3

Example 1: In this example we can see that by using d3.shuffle() method, we are able to get the shuffled array from the source array using Fisher-Yates shuffle algorithm and return the single array.

Javascript




// Defining d3 contrib variable  
var d3 = require('d3');
  
var gfg = d3.shuffle([1, 2, 3, 4, 5, 6]);
  
console.log(gfg);

Output :

[ 1, 5, 6, 3, 4, 2 ]

Example 2:

Javascript




// Defining d3 contrib variable  
var d3 = require('d3');
  
var gfg = d3.shuffle(["A", "B", "C"]);
  
console.log(gfg);

Output:

[ 'B', 'C', 'A' ]
My Personal Notes arrow_drop_up
Recommended Articles
Page :