Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffle nested arrays in Javascript

I'm trying to sort multiple arrays within an array (which also has to be shuffled). A simplified example is:

let toShuffle = [
[1, 2, 3, 4, 5],
[9, 8, 7, 6, 5],
[10, 67, 19 ,27]
...
];

const shuffled = shuffle(toShuffle);

// outout would look something like:
// [
//   [8, 6, 5, 7, 9],
//   [4, 3, 1, 5, 2],
//   [19, 26, 10, 67],
//   ...
// ]

This needs to be flexible, so any number of arrays with any amount of values should be valid.

Here is what I've tried:

function shuffle(a) {
  for (let e in a) {
    if (Array.isArray(a[e])) {
      a[e] = shuffle(a[e]);
    } else {
      a.splice(e, 1);
      a.splice(Math.floor(Math.random() * a.length), 0, a[e]);
    }
  }

  return a;
}

console.log("Shuffled: " + shuffle([
  [1, 2, 3, 4, 5],
  [5, 4, 3, 2, 1]
]))

But it's not working as intended. Is their an easier way to do this? Or is my code correct and just buggy.

like image 900
KoderM Avatar asked Sep 12 '25 08:09

KoderM


1 Answers

You can use Array.from() to create a new shallow-copied array and then to shuffle Array.prototype.sort() combined with Math.random()

Code:

const toShuffle = [
  [1, 2, 3, 4, 5],
  [9, 8, 7, 6, 5],
  [10, 67, 19 ,27]
]

const shuffle = a => Array.from(a).sort(() => .5 - Math.random())
const result = toShuffle.map(shuffle)

console.log('Shuffled:', JSON.stringify(result))
console.log('To shuffle:', JSON.stringify(toShuffle))
like image 169
Yosvel Quintero Arguelles Avatar answered Sep 14 '25 21:09

Yosvel Quintero Arguelles