【问题标题】:Delete duplicate elements from Array in Javascript [duplicate]在Javascript中从数组中删除重复元素[重复]
【发布时间】:2017-03-21 19:42:47
【问题描述】:

我有一个包含对象电子邮件和 ID 的数组,所以我想删除具有相似 ID 的重复元素。

例子:

var newarray=[
    {
        Email:"test1@gmail.com",
        ID:"A"
    },
    {
        Email:"test2@gmail.com",
        ID:"B"
    },
    {
        Email:"test3@gmail.com",
        ID:"A"
    },
    {
        Email:"test4@gmail.com",
        ID:"C"
    },
    {
        Email:"test4@gmail.com",
        ID:"C"
    }
];

现在我需要删除具有 ID 的重复元素很常见。因此我期望最终的数组是

var FinalArray=[
    {
        Email:"test1@gmail.com",
        ID:"A"
    },
    {
        Email:"test2@gmail.com",
        ID:"B"
    },  
    {
        Email:"test5@gmail.com",
        ID:"C"
    }
];

【问题讨论】:

  • 您只需要简单地创建一个新数组,遍历您的原始数组并用那些不重复的项目填充新数组。当然,你可能想通过使用 object 或 Map 或其他任何东西来提高这种性能,但想法是一样的。
  • 你期望test1 对应ID:"A"test5 对应ID:"C"。这是否意味着您不在乎过滤掉哪个重复项?
  • 为什么是'test5@gmail.com' 而不是'test4@gmail.com'
  • 感谢大家这么快的回答。
  • 为什么标记为重复???任何人都可以从链接的问题中找到解决此问题的任何代码吗??

标签: javascript arrays duplicates


【解决方案1】:

使用Array.prototype.filter 过滤元素并检查重复项,使用temp 数组

var newarray = [{
  Email: "test1@gmail.com",
  ID: "A"
}, {
  Email: "test2@gmail.com",
  ID: "B"
}, {
  Email: "test3@gmail.com",
  ID: "A"
}, {
  Email: "test4@gmail.com",
  ID: "C"
}, {
  Email: "test5@gmail.com",
  ID: "C"
}];
   
// Array to keep track of duplicates
var dups = [];
var arr = newarray.filter(function(el) {
  // If it is not a duplicate, return true
  if (dups.indexOf(el.ID) == -1) {
    dups.push(el.ID);
    return true;
  }

  return false;
  
});

console.log(arr);

【讨论】:

    【解决方案2】:

    您可以使用哈希表对其进行过滤。

    var newarray = [{ Email: "test1@gmail.com", ID: "A" }, { Email: "test2@gmail.com", ID: "B" }, { Email: "test3@gmail.com", ID: "A" }, { Email: "test4@gmail.com", ID: "C" }, { Email: "test5@gmail.com", ID: "C" }],
        filtered = newarray.filter(function (a) {
            if (!this[a.ID]) {
                this[a.ID] = true;
                return true;
            }
        }, Object.create(null));
    
    console.log(filtered);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    ES6 与 Set

    var newarray = [{ Email: "test1@gmail.com", ID: "A" }, { Email: "test2@gmail.com", ID: "B" }, { Email: "test3@gmail.com", ID: "A" }, { Email: "test4@gmail.com", ID: "C" }, { Email: "test5@gmail.com", ID: "C" }],
        filtered = newarray.filter((s => a => !s.has(a.ID) && s.add(a.ID))(new Set));
    
    console.log(filtered);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 伟大的 ES6 Set 解决方案!
    【解决方案3】:

    如果您可以使用 Javascript 库,例如 underscore 或 lodash,我建议您查看其库中的 _.uniq 函数。来自lodash:

    _.uniq(array, [isSorted=false], [callback=_.identity], [thisArg])
    

    这里你必须像下面这样使用,

    var non_duplidated_data = _.uniq(newarray, 'ID'); 
    

    【讨论】:

      【解决方案4】:

      使用Array.prototype.reduce 和哈希表的另一种解决方案 - 参见下面的演示:

      var newarray=[ { Email:"test1@gmail.com", ID:"A" }, { Email:"test2@gmail.com", ID:"B" }, { Email:"test3@gmail.com", ID:"A" }, { Email:"test4@gmail.com", ID:"C" }, { Email:"test5@gmail.com", ID:"C" } ];
      
      var result = newarray.reduce(function(hash){
        return function(prev,curr){
           !hash[curr.ID] && (hash[curr.ID]=prev.push(curr));
           return prev;
        };
      }(Object.create(null)),[]);
      
      console.log(result);
      .as-console-wrapper{top:0;max-height:100%!important;}

      【讨论】:

      • @SantoshKhavekar 让我知道(并投票!)如果这个答案对你有帮助,谢谢!
      猜你喜欢
      • 2013-05-20
      • 2011-07-03
      • 1970-01-01
      • 2011-08-17
      • 2010-10-13
      • 1970-01-01
      • 2020-05-08
      相关资源
      最近更新 更多