Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

Same order for two arrays randomization/shuffle in Javascript

I have a Javascript object that looks like this (not defined as a variable because it is within an array alongside other similar objects):

 { //my_dict
  fruit: [[Apple, Banana, Grapes, Apple, Grapes, Grapes], [Banana, Apple, Apple, Kiwi, Grapes, Banana]],
  drink: [[smoothie, juice],[juice]],    
 },

I need to take one of the arrays in fruit (randomly) and assign it to a new key fruit_left: within my object and the other one to a new key fruit_right:. I also need to assign each of the arrays in drink: to new keys, say drink_left: and drink_right:. Now, the important thing is that I need to preserve the order so that if the first array in the original array fruit: becomes my fruit_left, then the first list in the original array drink: gets assigned to drink_left:, and vice versa.

Example result:

    { //my_dict
          fruit: [[Banana, Apple, Apple, Kiwi, Grapes, Banana], [Apple, Banana, Grapes, Apple, Grapes, Grapes]]
          drink: [[juice],[smoothie, juice]], 
          fruit_left: [Banana, Apple, Apple, Kiwi, Grapes, Banana],
          fruit_right: [Apple, Banana, Grapes, Apple, Grapes, Grapes],
          drink_left: [juice],
          drink_right: [smoothie, juice] 
         },

I know it might sound confusing, but the way I have stored my data makes sense within the rest of my script, so I would really prefer to leave it that way. I was thinking of shuffling a list and then picking item [0] for my left side and [1] for my right side, but then I don't know how to make sure the drinks are shuffled in the same exact way. Is there a way to parallel shuffle? Both fruit and drink only have 2 items. Or perhaps is there a way to store what the original fruit array was and then use that information to infer the new order of drink? Thank you so much!

Note: I am trying to shuffle the order of the arrays, not the order of the items within them.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am trying to shuffle the order of the arrays, not the order of the items within them

so using the shuffle method from here. you just want this:

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

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

const my_dict = { //my_dict
          fruit: [['Banana', 'Apple', 'Apple', 'Kiwi', 'Grapes', 'Banana'], ['Apple', 'Banana', 'Grapes', 'Apple', 'Grapes', 'Grapes']]
         };
         
 const shuffledFruit = shuffle(my_dict.fruit);
 
 console.log(shuffledFruit);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...