使用索引从已排序的多3元素数组中删除重复项

时间:2018-04-05 08:05:23

标签: javascript arrays loops

我希望以三个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();

2 个答案:

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

<强>演示:

这是一个在您的代码中使用此功能的演示版:

&#13;
&#13;
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;
&#13;
&#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
}

<强>演示

&#13;
&#13;
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;
&#13;
&#13;