在Javascript中,两个数组的随机化/混洗顺序相同



我有一个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],但我不知道如何确保饮料以完全相同的方式打乱。有没有并行洗牌的方法?水果和饮料都只有两项。或者,有没有一种方法可以存储最初的水果阵列,然后使用这些信息来推断新的饮料顺序?非常感谢!

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

我试图打乱数组的顺序,而不是中的项目

因此从这里使用shuffle方法。你只想要这个:

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

或者,如果您不想实际更改数组本身,只需先复制它。

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

我通过创建所需的键并在我的对象中为它们赋值来实现这一点,如下所示:

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

相关内容

  • 没有找到相关文章

最新更新