-2

How can I loop through all the entries in an array using JavaScript? I have an array and i want an another array of randoms list in arrRand variable and then execute my further code .

 list1 = ['aceClub', 'twoClub'];
 list2 = ['fourDiamond', 'aceClub', 'fourHeart', 'twoDiamond'];
 list3 = ['aceHeart', 'aceClub', 'aceHeart', 'aceDiamond'];
 list4 = ['aceDiamond', 'fourSpade', 'twoClub', 'fourHeart'];
 list5 = ['fourClub', 'twoSpade' , 'twoHeart', 'aceDiamond'];
 list6 = ['twoHeart', 'twoDiamond' , 'twoSpade' , 'aceClub'];

 var arr = [list1,list2,list3,list4, list5,list6];

i want this in array of randoms .

var arrRand = [list5,list6,list2,list4, list3,list1];
var arrRand = [list3,list2,list6,list5, list1,list4];
var arrRand = [list1,list5,list2,list4, list6,list2];

and then

if(arrRand[0] == list1){
   //mycode 
}
if(arrRand[1] == list2){
  //myCode
}
....
Shubham Seth
  • 77
  • 1
  • 1
  • 5

2 Answers2

1

The standard shuffle for Arrays in Javascript seems to be the Fisher-yates shuffle - from this answer

let list1 = ['aceClub', 'twoClub'];
let list2 = ['fourDiamond', 'aceClub', 'fourHeart', 'twoDiamond'];
let list3 = ['aceHeart', 'aceClub', 'aceHeart', 'aceDiamond'];
let list4 = ['aceDiamond', 'fourSpade', 'twoClub', 'fourHeart'];
let list5 = ['fourClub', 'twoSpade', 'twoHeart', 'aceDiamond'];
let list6 = ['twoHeart', 'twoDiamond', 'twoSpade', 'aceClub'];

let arr = [list1, list2, list3, list4, list5, list6];

let arrRand = shuffle(arr);
console.log(arrRand);

function shuffle(array) {
  let currentIndex = array.length;
  let temporaryValue;
  let randomIndex;

  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
Shiny
  • 4,945
  • 3
  • 17
  • 33
0

I think what you want to do is shuffle the array. You can use the sort method together with randomly generated number as it's comparison function:

 list1 = ['aceClub', 'twoClub'];
 list2 = ['fourDiamond', 'aceClub', 'fourHeart', 'twoDiamond'];
 list3 = ['aceHeart', 'aceClub', 'aceHeart', 'aceDiamond'];
 list4 = ['aceDiamond', 'fourSpade', 'twoClub', 'fourHeart'];
 list5 = ['fourClub', 'twoSpade' , 'twoHeart', 'aceDiamond'];
 list6 = ['twoHeart', 'twoDiamond' , 'twoSpade' , 'aceClub'];

 var arr = [list1,list2,list3,list4, list5,list6];

 const shuffle = arr => arr.sort(() => Math.random() - 0.5);

 console.log(shuffle(arr))   //different array order on each fn call
 console.log(shuffle(arr))
 ...
Addis
  • 2,480
  • 2
  • 13
  • 21
  • `sort(() => Math.random() - 0.5)` isn't particularly random. It'll tend towards keeping the first elements closer to the front – Shiny Nov 23 '19 at 22:32
  • @Light that depends on the output of the random number. I have also checked a few samples and it was ok. – Addis Nov 23 '19 at 22:37
  • As you said the Fisher–Yates shuffle method is the standard and more accurate way. This one is more concise and could be used for a solution that needs less accuracy. – Addis Nov 23 '19 at 23:01
  • 1
    Fair, I was perhaps being overly pedantic – Shiny Nov 23 '19 at 23:06