-1

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.

gimi
  • 395
  • 3
  • 13
  • 1
    do you have an example of the wanted result? – Nina Scholz Aug 21 '20 at 08:48
  • Does this answer your question? [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) – Liam Aug 21 '20 at 08:48
  • I'm not sure what the Parallel bit is but there is but Js is single threaded so you can't do parallel processing – Liam Aug 21 '20 at 08:49
  • Are you not getting a [sense of de ja vue](https://stackoverflow.com/a/63424747/542251) @NinaScholz? – Liam Aug 21 '20 at 08:50
  • @Liam, I think my question is similar, but different from both the ones you linked – gimi Aug 21 '20 at 08:59
  • you seem to be tying yourself in knots here. The object you want is bit of a mess, people would be able to answer your question(s) much better if you actually explain what you want to achieve. At the moment you keep asking [XY problems](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) which is why you need to keep asking over and over. If you provide context then people will be able to provide better answers rather than everyone going though this convoluted guessing game – Liam Aug 21 '20 at 09:19
  • It seems you actually just want [to only show 3 random fruits among the 6 listed in fruit](https://stackoverflow.com/questions/63355322/remove-random-html-images-from-a-javascript-variable/63373294)? – Liam Aug 21 '20 at 09:21
  • @Liam I thought I made it clear in my note that "I am trying to shuffle the order of the arrays, not the order of the items within them." Sorry if that wasn't clear. The scripts are part of a much bigger and complicated game, which I'd rather not get into if possible. I am new to Javascript and stack overflow, my apologies if I am not making the best use out of it... – gimi Aug 21 '20 at 10:13
  • If you want to shuffle the array then you should read [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array). An array of arrays is still an array. – Liam Aug 21 '20 at 10:26

2 Answers2

0

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

Or if you don't want to actually alter the array itself, just copy it first.

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.slice());
 
 console.log(shuffledFruit);
Liam
  • 27,717
  • 28
  • 128
  • 190
-1

I managed to do it by creating the desired keys and assigning them null values within my object like so:

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

Then (supposing my object is in a "my_dicts" array):

for (var t = 0; t < my_dicts.length; t++) {
// first, shuffle the copy; then determine which one is left and which one is right, and assign drinks accordingly
shuffle(my_dicts[t].fruit_shuffled); 
my_dicts[t]["fruit_left"] = my_dicts[t]["fruit_shuffled"][0];
my_dictss[t]["fruit_right"] = my_dicts[t]["fruit_shuffled"][1];
if (my_dicts[t]["fruit_left"] == my_dicts[t]["fruit"][0]) {
    my_dicts[t]["drink_left"] = my_dicts[t]["drink"][0],
    my_dicts[t]["drink_right"] = my_dicts[t]["drink"][1];
} else {
    my_dicts[t]["drink_left"] = my_dicts[t]["drink"][1],
    my_dicts[t]["drink_right"] = my_dicts[t]["drink"][0];
};
Liam
  • 27,717
  • 28
  • 128
  • 190
gimi
  • 395
  • 3
  • 13
  • *first, shuffle the copy; then determine which one is left and which one is right, and assign drinks accordingly* I'm still positive your overthinking this but if your not willing to go into details . This code still looks like a mess... – Liam Aug 21 '20 at 10:28
  • If your going to answer your own question at least add what `shuffle` does, being as this is what your asking... – Liam Aug 21 '20 at 10:30