Javascript中两个数组随机化/随机播放的顺序相同 [英] Same order for two arrays randomization/shuffle in Javascript

查看:81
本文介绍了Javascript中两个数组随机化/随机播放的顺序相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的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]],    
 },

我需要采用<$中的一个数组c $ c> fruit (随机),并将其分配给对象中的新键 fruit_left:,另一个分配给新键 fruit_right:。我还需要将 drink:中的每个数组分配给新键,例如 drink_left: drink_right:。现在,重要的是我需要保留订单,以便如果原始数组 fruit:中的第一个数组成为我的 fruit_left ,然后将原始数组 drink:中的第一个列表分配给 drink_left:,反之亦然。

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.

示例结果:

    { //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] 
         },

我知道这听起来可能令人困惑,但是在脚本的其余部分中,我存储数据的方式很有意义,所以我真的更喜欢保留它。
我本来打算改组列表,然后在左侧选择项目[0],在右侧选择项目[1],但是我不知道如何确保以相同的方式对饮料进行重新混合办法。有没有办法并行洗牌?水果和饮料都只有2种。或者,也许有一种方法可以存储原始的水果阵列,然后使用该信息来推断新的饮料顺序?
非常感谢!

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.

推荐答案


我试图改组数组的顺序,而不是数组中
项的顺序

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

这篇关于Javascript中两个数组随机化/随机播放的顺序相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆