【问题标题】:Same order for two arrays randomization/shuffle in JavascriptJavascript中两个数组随机化/随机播放的相同顺序
【发布时间】:2020-12-10 15:27:01
【问题描述】:

我有一个 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 项。或者也许有办法存储原始水果数组是什么,然后使用该信息来推断新的饮料顺序? 非常感谢!

注意:我正在尝试打乱数组的顺序,而不是其中项目的顺序。

【问题讨论】:

  • 你有想要的结果的例子吗?
  • 这能回答你的问题吗? Getting a random value from a JavaScript array
  • 我不确定 Parallel 位是什么,但是有,但是 Js 是单线程的,所以你不能进行并行处理
  • 你没有收到sense of de ja vue@NinaScholz 吗?
  • @Liam,我认为我的问题很相似,但与您链接的两个问题不同

标签: javascript arrays object random shuffle


【解决方案1】:

我正在尝试打乱数组的顺序,而不是 其中的项目

所以使用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);

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 首先,洗牌;然后确定哪一个是剩下的,哪一个是正确的,并相应地分配饮料我仍然肯定你的过度思考,但如果你不愿意详细说明。这段代码看起来还是一团糟……
    • 如果您要回答自己的问题,至少添加 shuffle 所做的事情,因为这就是您要问的......
    猜你喜欢
    • 2011-01-27
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    相关资源
    最近更新 更多