Javascript中两个数组随机化/随机播放的顺序相同

时间:2020-08-21 08:47:02

标签: javascript arrays object random shuffle

我有一个看起来像这样的Javascript对象(未定义为变量,因为它在数组中与其他类似对象并排放置):

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

我需要(随机地)获取fruit中的一个数组,并将其分配给对象内的新键fruit_left:,并将另一个分配给新键fruit_right:。我还需要将drink:中的每个数组分配给新键,例如drink_left:drink_right:。现在,重要的是,我需要保留顺序,以便如果原始数组fruit:中的第一个数组成为我的fruit_left,那么原始数组中的第一个列表数组drink:被分配给drink_left:,反之亦然。

示例结果:

    { //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种。或者,也许有一种方法可以存储原始的水果阵列,然后使用该信息来推断新的饮料顺序? 非常感谢!

注意:我试图改组数组的顺序,而不是其中的项目的顺序。

2 个答案:

答案 0 :(得分:0)

我正在尝试改组数组的顺序,而不是顺序 其中的物品

因此使用shuffle method from here。你只想要这个:

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

或者,如果您不想实际更改数组本身,请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);

答案 1 :(得分:-1)

我设法通过创建所需的键并在对象中为它们分配空值来做到这一点,就像这样:

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

然后(假设我的对象在“ my_dicts”数组中):

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];
};