【问题标题】:remove duplicate array from array list using filter?使用过滤器从数组列表中删除重复数组?
【发布时间】:2016-06-10 07:20:20
【问题描述】:

我用这个代码

labels.map((label, key) => {
  const xAxis = (parseInt(label, 10)).toFixed(0);
  const yAxis = parseInt(this.props.dataSet.datasets[0].data[key], 10);
  const arrayAxis = [xAxis, yAxis];
  data.push(arrayAxis);
  return arrayAxis;
});

然后我得到数据列表

["1", 1] 
["2", 1]  // duplicate
["2", 1]  // duplicate
["3", 1] 
["4", 1] 
["4", 1] 
["5", 4] 
["6", 4] 
["7", 4]
["8", 4]
["9", 4]
.....
....
...

如何使用过滤器从数组列表中删除重复数组?

【问题讨论】:

  • 什么是重复?请添加label的原始数据。
  • @NinaScholz U 可以在数据列表中看到
  • 数据是否有序?
  • 你的数据是有序的

标签: javascript arrays filter ecmascript-6


【解决方案1】:

您可以将其与临时对象一起使用以指示重复项。它也适用于未排序的数组。

levels.forEach((temp => (label, key) => {
    var xAxis = (parseInt(label, 10)).toFixed(0),
        yAxis = parseInt(this.props.dataSet.datasets[0].data[key], 10),
        arrayAxis =[ xAxis, yAxis];
    if (!temp[arrayAxis]) {
        temp[arrayAxis] = true;
        data.push(arrayAxis);
    }
})(Object.create(null)));

【讨论】:

    【解决方案2】:

    您可以使用过滤器来做到这一点,但使用lodashuniqByisEqual 函数会更简单。

    _.uniqWith(arr, _.isEqual);
    

    你可以在这里阅读:https://lodash.com/docs#uniqByhttps://lodash.com/docs#isEqual

    请注意,它确实涉及添加对 lodash 的依赖。

    【讨论】:

      【解决方案3】:

      使用这个:

      var data = [["1", 1],
      ["2", 1],
      ["3", 1],
      ["4", 1],
      ["5", 4],
      ["6", 4],
      ["7", 4],
      ["8", 4],
      ["9", 4]];
      
      const xAxis = "2";
      const yAxis = 1;
      
      const arrayAxis = [xAxis, yAxis];
      if($.inArray(arrayAxis[xAxis], data) !== -1)
      {
          data.push(arrayAxis);
      }
      console.log(data);
      

      此代码可防止您的重复。

      【讨论】:

        【解决方案4】:

        如果可能的话,我总是喜欢提供一个通用的答案,而不是针对具体问题量身定制的答案。

        对我来说,这个问题是关于根据不是简单的属性而是类似的数组或对象来过滤数组或数组的对象项。我将使用我以前在其他一些情况下使用过的两种实用方法。 Object.prototype.compare()Array.prototype.compare()。让我们看看。

        Object.prototype.compare = function(o){
          var ok = Object.keys(this);
          return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
        };
        Array.prototype.compare = function(a){
          return this.every((e,i) => typeof a[i] === "object" ? a[i].compare(e) : a[i] === e);
        };
        
        var data = [["1", 1],["2", 1],["2", 1],["3", 1],["4", 1],["4", 1],["5", 4],["6", 4],["7", 4],["8", 4], ["9", 4]],
        filtered = data.filter(function(e) { var f = this.find(f => f.compare(e));
                                             return !f ? this.push(e) : void 0;
                                           },[]);
        console.log(JSON.stringify(filtered));

        【讨论】:

          【解决方案5】:
          let res = [...new Set([1,3,45, 1, 3])]
          

          输出为 = [1,3,45]

          【讨论】:

            猜你喜欢
            • 2020-11-24
            • 2018-12-07
            • 2019-10-16
            • 2022-01-20
            • 2014-07-31
            • 2016-04-26
            • 2023-01-02
            • 2015-01-06
            • 1970-01-01
            相关资源
            最近更新 更多