Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...