【问题标题】:javascript: how to remove duplicate arrays inside array of arraysjavascript:如何删除数组数组中的重复数组
【发布时间】:2017-10-16 07:44:43
【问题描述】:

输入:

[[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]]

我想要的输出:

[[-1,-1,2],[-1,0,1]]

除了this one之外还有其他想法吗?

谢谢

【问题讨论】:

  • 为什么我们不应该将其作为您自己链接的问题的副本关闭?该问题的解决方案对您不起作用吗?
  • 同意。这看起来是个不错的解决方案。
  • @nnnnnn 因为这是一种可怕而缓慢的方法?
  • 它的语义令人担忧。 toString 真的可以为所有值定义身份吗?
  • 如果您的数据实际上比整数更复杂,您可以使用这种方法进行比较:stackoverflow.com/questions/7837456/…

标签: javascript


【解决方案1】:

您不会真正绕过对数组进行字符串化,因为这是按值比较它们的最简单(并且相当快)的方法。所以我会去

Array.from(new Set(input.map(JSON.stringify)), JSON.parse)

另请参阅Remove Duplicates from JavaScript Array 了解其他方法,尽管其中大多数方法需要两个值才能与=== 进行比较。

【讨论】:

  • 虽然我同意这可行,但我担心JSON.stringify() 将从.map() 收到的第二个和第三个参数在某些实现中会干扰它,因为第二个参数应该可以是null 或函数,第三个参数应该是数字或字符串。
【解决方案2】:

魔法

d.filter(( t={}, a=> !(t[a]=a in t) ));

我假设您的输入数据在数组d 中。解释here

let d = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]];

var r = d.filter((t={},a=>!(t[a]=a in t)));

console.log(JSON.stringify(r));

【讨论】:

  • 有时我必须先声明 t,有时我没有,太奇怪了。
  • 当它喊出 t 的引用错误时,看起来就像它的严格模式
【解决方案3】:

已经有一个很好的实用程序,试试Lodash,它的功能之一是_.uniqWith,使用该功能您可以执行以下操作。

<script src="/path/to/lodash.js"></script>
<script>
    var aa = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]];
    console.log(aa);
    console.log(_.uniqWith(aa,_.isEqual));
</script>

【讨论】:

    【解决方案4】:

    您可以创建一个 hashMap 并在其中保存值。这将始终保持最后一个值。

    var data = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]]
    
    var hashMap = {}
    
    data.forEach(function(arr){
      // If your subArrays can be in any order, you can use .sort to have consistant order
      hashMap[arr.join("|")] = arr;
    });
    
    var result = Object.keys(hashMap).map(function(k){
      return hashMap[k]
    })
    
    console.log(result)

    【讨论】:

      【解决方案5】:

      jsfiddle

      post借用数组比较代码

      // Warn if overriding existing method
      if(Array.prototype.equals)
          console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
      // attach the .equals method to Array's prototype to call it on any array
      Array.prototype.equals = function (array) {
          // if the other array is a falsy value, return
          if (!array)
              return false;
      
          // compare lengths - can save a lot of time 
          if (this.length != array.length)
              return false;
      
          for (var i = 0, l=this.length; i < l; i++) {
              // Check if we have nested arrays
              if (this[i] instanceof Array && array[i] instanceof Array) {
                  // recurse into the nested arrays
                  if (!this[i].equals(array[i]))
                      return false;       
              }           
              else if (this[i] != array[i]) { 
                  // Warning - two different object instances will never be equal: {x:20} != {x:20}
                  return false;   
              }           
          }       
          return true;
      }
      
      var old = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]], n = [];
      
      while(old.length) {
          var arr = old.shift(), matched = false;
      
        for(var i = 0, len = n.length; i < len; i++) {
          if (arr.equals(n[i])) {
              matched = true;
              break;
          }
        }
        if (!matched) {
          n.push(arr);
        }
      }
      

      【讨论】:

      猜你喜欢
      • 2019-08-29
      • 2014-12-03
      • 2020-11-01
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多