如何在Javascript中从二维数组中过滤重复的整数和字符串子数组

时间:2021-10-29 12:47:33

I am receiving a 2-dimensional array of both integers and strings, and I want to remove duplicates from them:

我收到一个包含整数和字符串的二维数组,我想从中删除重复项:

original array = [["admin", 2, "regular"], ["customer", "regular"], ["regular", "customer"], [1], ,["admin"], [1], ["admin"]

original array = [[“admin”,2,“regular”],[“customer”,“regular”],[“regular”,“customer”],[1] ,, [“admin”],[1] ,[“admin”]

expected result = [["admin", 2, "regular"], ["customer", "regular"], [1], ["admin"]]

预期结果= [[“admin”,2,“常规”],[“客户”,“常规”],[1],[“管理员”]]

Please how can I do this in Javascript?

请问如何在Javascript中执行此操作?

3 个解决方案

#1


1  

Does it matter if your array (and it's sub-arrays) gets reordered? If it doesn't, then:

你的数组(和它的子数组)是否重新排序是否重要?如果没有,那么:

var array = [["admin", 2, "regular"], ["customer", "regular"], ["regular", "customer"], [1],["admin"], [1], ["admin"]];

array = array.map(x => x.sort()).sort();

var uniqueArray = [];
uniqueArray.push(array[0]);

for (var i = 1; i < array.length; i++){
  if (JSON.stringify(array[i]) != JSON.stringify(array[i-1])){
      uniqueArray.push(array[i]);
      }
}

console.log(uniqueArray);

#2


1  

You could sort a copy from the inner arrays and build a string and check against a hash table while filtering.

您可以从内部数组中对副本进行排序并构建字符串,并在过滤时检查哈希表。

var array = [["admin", 2, "regular"], ["customer", "regular"], ["regular", "customer"], [1], ["admin"], [1], , ["admin"]],
    object = Object.create(null),
    unique = array.filter(function (a) {
        var b = a.slice().sort().join('|');
        return !object[b] && (object[b] = true)
    });
    
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#3


0  

Here is an algorithm that might help you. It is NOT javascript written. And it does not do the difference between ["customer", "regular"] and ["regular", "customer"], but you have the general idea :

这是一个可能对您有帮助的算法。它不是javascript写的。它并没有区分[“客户”,“常规”]和[“常规”,“客户”],但你有一般的想法:

var newArray = [];
for(x in originalArray) { //travel in the original array
var flag = 1; //flag indicating if x is already in newArray
   for(y in array) { 
     if(y == x) flag = 0; //if x already belongs to newArray
   }
   if(flag==1) newArray += x; //add if not already in newArray
flag = 1; //reset value of flag
}

Basically, you create a new array in which you add only value that do not already belong to it. Good luck :)

基本上,您创建一个新数组,在该数组中只添加不属于它的值。祝你好运 :)

#1


1  

Does it matter if your array (and it's sub-arrays) gets reordered? If it doesn't, then:

你的数组(和它的子数组)是否重新排序是否重要?如果没有,那么:

var array = [["admin", 2, "regular"], ["customer", "regular"], ["regular", "customer"], [1],["admin"], [1], ["admin"]];

array = array.map(x => x.sort()).sort();

var uniqueArray = [];
uniqueArray.push(array[0]);

for (var i = 1; i < array.length; i++){
  if (JSON.stringify(array[i]) != JSON.stringify(array[i-1])){
      uniqueArray.push(array[i]);
      }
}

console.log(uniqueArray);

#2


1  

You could sort a copy from the inner arrays and build a string and check against a hash table while filtering.

您可以从内部数组中对副本进行排序并构建字符串,并在过滤时检查哈希表。

var array = [["admin", 2, "regular"], ["customer", "regular"], ["regular", "customer"], [1], ["admin"], [1], , ["admin"]],
    object = Object.create(null),
    unique = array.filter(function (a) {
        var b = a.slice().sort().join('|');
        return !object[b] && (object[b] = true)
    });
    
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#3


0  

Here is an algorithm that might help you. It is NOT javascript written. And it does not do the difference between ["customer", "regular"] and ["regular", "customer"], but you have the general idea :

这是一个可能对您有帮助的算法。它不是javascript写的。它并没有区分[“客户”,“常规”]和[“常规”,“客户”],但你有一般的想法:

var newArray = [];
for(x in originalArray) { //travel in the original array
var flag = 1; //flag indicating if x is already in newArray
   for(y in array) { 
     if(y == x) flag = 0; //if x already belongs to newArray
   }
   if(flag==1) newArray += x; //add if not already in newArray
flag = 1; //reset value of flag
}

Basically, you create a new array in which you add only value that do not already belong to it. Good luck :)

基本上,您创建一个新数组,在该数组中只添加不属于它的值。祝你好运 :)