Friday, October 24, 2025
HomeLanguagesJavascriptD3.js shuffle() method

D3.js shuffle() method

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:

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 the Fisher-Yates shuffle algorithm and return the single array.

Javascript




// Defining d3 contrib variable
const d3 = require('d3');
 
const 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
const d3 = require('d3');
 
const gfg = d3.shuffle(["A", "B", "C"]);
 
console.log(gfg);


Output:

[ 'B', 'C', 'A' ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS