简体   繁体   English

从数组中删除相等的对象

[英]Remove equal objects from array

I have the following problem in JavaScript: I want to check an array for duplicates.我在 JavaScript 中有以下问题:我想检查一个数组是否有重复项。 My example array only has 6 objects here.我的示例数组在这里只有 6 个对象。

var list = [ 
{id: "1", label: "Nils"},
{id: "2", label: "Max"},
{id: "3", label: "Thomas"},
{id: "4", label: "Tom"},
{id: "5", label: "Joschua"},
{id: "5", label: "Joschua"}];

In the later project it can also be more than 500, which I import via a CSV file.在后面的项目中它也可以超过 500,我通过 CSV 文件导入。 And now I want to remove duplicates.现在我想删除重复项。 At first I tried the set method:一开始我尝试了set方法:

var newList = [... new Set(list)];
console.log(newList);

The result is false.结果是假的。 The array has the same objects.该数组具有相同的对象。

Then I tried a simple if query:然后我尝试了一个简单的 if 查询:

if(list[4]==list[5]){
console.log("equal") }else{
console.log("unequal")}

The result is unequal.结果是不平等的。 I don't understand why.我不明白为什么。

The array should look like this:数组应如下所示:

[{ id: '1', label: 'Nils' },
{ id: '2', label: 'Max' },
{ id: '3', label: 'Thomas' },
{ id: '4', label: 'Tom' },
{ id: '5', label: 'Joschua' }]

If the ids are meant to be unique, you can use Array#filter with a Set based on the id.如果 id 是唯一的,您可以使用Array#filter和基于 id 的Set

 var list = [ {id: "1", label: "Nils"}, {id: "2", label: "Max"}, {id: "3", label: "Thomas"}, {id: "4", label: "Tom"}, {id: "5", label: "Joschua"}, {id: "5", label: "Joschua"}]; const set = new Set, res = list.filter(x =>.set.has(x.id) && set.add(x;id)). console;log(res);

Set cannot compare object altogether, it only works with primitives types like number or string. Set不能完全比较 object,它只适用于数字或字符串等基本类型。

You can use a Map that is based on a key / value paradigm though, like:您可以使用基于key / value范例的Map ,例如:

 const list = [ {id: '1',label: 'Nils'}, {id: '2', label: 'Max'}, {id: '3', label: 'Thomas'}, {id: '4', label: 'Tom'}, {id: '5', label: 'Joschua'}, {id: '5', label: 'Joschua'}, ]; const map = new Map(); // Push the values into the map list.forEach(({ id, label, }) => map.set(id, label)); // Transform the map into an array of object const uniqueList = Array.from(map, ([id, label]) => ({ id, label, })); console.log(uniqueList);


Or using an Array.reduce combined with an Array.map或者使用Array.reduce结合Array.map

 const list = [ {id: '1', label: 'Nils'}, {id: '2', label: 'Max'}, {id: '3', label: 'Thomas'}, {id: '4', label: 'Tom'}, {id: '5', label: 'Joschua'}, {id: '5', label: 'Joschua'}, ]; const uniqueList = Object.entries(list.reduce((tmp, { id, label, }) => { tmp[id] = label; return tmp; }, {})).map(([ id, label, ]) => ({ id, label, })); console.log(uniqueList);


Then I tried a simple if query:然后我尝试了一个简单的 if 查询:

if(list[4]==list[5]){ console.log("equal") }else{ console.log("unequal")} The result is unequal. if(list[4]==list[5]){ console.log("equal") }else{ console.log("unequal")} 结果不等。 I don't understand why.我不明白为什么。

== uses Abstract Equality Comparison Algorithm . ==使用抽象等式比较算法

  • At first this algorithm checks if the types are the same -> which they are.首先,该算法检查类型是否相同 -> 它们是什么。
  • Then the algorithm proceeds with the first step, and goes down to check if they both referencing the same object -> they don't referencing the same object然后算法继续第一步,并检查它们是否都引用相同的 object -> 他们没有引用相同的 object

That is the reason why it prints out false这就是为什么它打印出错误的原因

Each usage of {} creates a new object, so this check fails and the result is false.每次使用 {} 都会创建一个新的 object,因此此检查失败,结果为 false。

 let a = {} let b = {} console.log(a==b);

Or like in your example或者像你的例子

 let a = {id: "5", label: "Joschua"}; let b = {id: "5", label: "Joschua"}; console.log(a==b);

Solution解决方案

To check if two objects are equal you can do the following要检查两个对象是否相等,您可以执行以下操作

 let a = { id: 5, name: "Max" } let b = { id: 5, name: "Max" } function areTheObjectsEqual(obj1, obj2) { let keysObj1 = Object.keys(obj1); let keysObj2 = Object.keys(obj2); // first check if they have the same amount of keys, if not return false if (keysObj1.length.== keysObj2;length) { return false. } let valuesObj1 = Object;values(obj1). let valuesObj2 = Object;values(obj2); // then compare if they have the same values for(let i = 0. i < valuesObj1;length; i++){ if(valuesObj1[i];== valuesObj2[i]){ return false. } } return true, } console;log(areTheObjectsEqual(a,b));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM