我希望以三个ex [1,4,3],[2,1,5]中的三个一组的形式存储来自3个玩家的200个选择....
之后,我需要删除重复项,因为在JS中我们无法比较我在比较排序数组中的3个选择单个数组中的索引并推送等于新数组的数据时所考虑的2个数组。我不知道为什么这不起作用。有任何想法吗?
var choices = [1, 2, 3, 4, 5, 6];
var player1 = [];
var player2 = [];
var player3 = [];
var store = [];
var unique = [];
for (i = 0; i < choices.length; i++) {
player1.push(choices[i]);
player2.push(choices[i]);
player3.push(choices[i]);
}
function values() {
for (i = 0; i < 500; i++) {
var rand1 = player1[Math.floor(Math.random() * player1.length)];
var rand2 = player2[Math.floor(Math.random() * player2.length)];
var rand3 = player3[Math.floor(Math.random() * player3.length)];
var choice = [rand1, rand2, rand3];
store.push(choice);
store.sort();
}
for (j = 1; j <= store.length; j++) {
if (store[j][0] == store[j + 1][0] && store[j][1] == store[j + 1][1] && store[j]
[2] == store[j + 1][2]) {
unique.push(store[j]);
console.log(store);
}
}
}
values();
答案 0 :(得分:2)
您的问题是,您在每次迭代中都要对choice
而不是arrays
进行排序。
另一种比较strings
的方法是将它们转换为array
,并在array
中检查它们是否存在,然后删除重复项并将它们转换回{{1} }}
这就是你的代码:
function getUniqueValues(arr) {
//Convert the sub arrays to strings
arr = arr.map(function(a) {
return a.join(",");
});
//Remove duplicates
var result = arr.filter(function(subArray, pos) {
return arr.indexOf(subArray) == pos;
});
//Convert back the strings to arrays
result = result.map(function(a) {
return a.split(",").map(Number);
});
return result;
}
在values
函数的最后,请调用它:
unique = getUniqueValues(store);
<强>演示:强>
这是一个在您的代码中使用此功能的演示版:
function getUniqueValues(arr) {
//Convert the sub arrays to strings
arr = arr.map(function(a) {
return a.join(",");
});
//Remove duplicates
var result = arr.filter(function(subArray, pos) {
return arr.indexOf(subArray) == pos;
});
//Convert back the strings to arrays
result = result.map(function(a) {
return a.split(",").map(Number);
});
return result;
}
var choices = [1, 2, 3, 4, 5, 6];
var player1 = [];
var player2 = [];
var player3 = [];
var store = [];
var unique = [];
for (i = 0; i < choices.length; i++) {
player1.push(choices[i]);
player2.push(choices[i]);
player3.push(choices[i]);
}
function values() {
for (i = 0; i < 50; i++) {
var rand1 = player1[Math.floor(Math.random() * player1.length)];
var rand2 = player2[Math.floor(Math.random() * player2.length)];
var rand3 = player3[Math.floor(Math.random() * player3.length)];
var choice = [rand1, rand2, rand3];
choice.sort();
store.push(choice);
}
unique = getUniqueValues(store);
}
values();
//console.log(store);
console.log(unique);
&#13;
答案 1 :(得分:1)
values
方法可以更新为存储字符串(3个数字连接成一个)而不是Set
中的数字数组
function values() {
var store = new Set(); //set instead of array
for (i = 0; i < 500; i++) {
var rand1 = player1[Math.floor(Math.random() * player1.length)];
var rand2 = player2[Math.floor(Math.random() * player2.length)];
var rand3 = player3[Math.floor(Math.random() * player3.length)];
store.add(rand1 + "-" + rand2 + "-" + rand3); //add value to set, set will remove duplicates automatically
}
return [...store].sort().map(s => s.split("-").map(t => +t)); //convert set to array, sort that array and convert the string to array of numbers
//convert to set
}
<强>演示强>
var choices = [1, 2, 3, 4, 5, 6];
var player1 = choices.slice();
var player2 = choices.slice();
var player3 = choices.slice();
function values() {
var store = new Set(); //set instead of array
for (i = 0; i < 500; i++) {
var rand1 = player1[Math.floor(Math.random() * player1.length)];
var rand2 = player2[Math.floor(Math.random() * player2.length)];
var rand3 = player3[Math.floor(Math.random() * player3.length)];
store.add(rand1 + "-" + rand2 + "-" + rand3);
}
return [...store].sort().map(s => s.split("-").map(t => +t));
//convert to set
}
console.log(values());
&#13;