在Javascript中删除Array中的重复元素[重复]

时间:2021-08-10 21:17:58

This question already has an answer here:

这个问题在这里已有答案:

I have array with obejcts email and Id so I want delete duplicate elements who have similar ID's.

我有一个带有obejcts电子邮件和Id的数组,所以我想要删除具有类似ID的重复元素。

Example:

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"
    }
];

Now I need to delete Duplicate elements which have ID's are common.In the sence I am expecting final Array is

现在我需要删除ID很常见的重复元素。我期待最终的数组是

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

4 个解决方案

#1


5  

Use Array.prototype.filter to filter out the elements and to keep a check of duplicates use a temp array

使用Array.prototype.filter过滤掉元素并使用临时数组检查重复项

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


3  

You could filter it with a hash table.

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

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 with Set

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; }

#3


2  

If you can use Javascript libraries such as underscore or lodash, I recommend having a look at _.uniq function in their libraries. From lodash:

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

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

Here you have to use like below,

在这里你必须使用如下,

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

#4


1  

Another solution using Array.prototype.reduce and a hash table - see demo below:

使用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;}

#1


5  

Use Array.prototype.filter to filter out the elements and to keep a check of duplicates use a temp array

使用Array.prototype.filter过滤掉元素并使用临时数组检查重复项

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


3  

You could filter it with a hash table.

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

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 with Set

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; }

#3


2  

If you can use Javascript libraries such as underscore or lodash, I recommend having a look at _.uniq function in their libraries. From lodash:

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

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

Here you have to use like below,

在这里你必须使用如下,

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

#4


1  

Another solution using Array.prototype.reduce and a hash table - see demo below:

使用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;}