简体   繁体   English

在 Javascript 中传播没有重复的元素

[英]Spreading elements with no duplicates in Javascript

I am looking for the way to remove duplicates.我正在寻找删除重复项的方法。 I found a common way is to create a Set and then spread into a new Array.我发现一种常见的方法是创建一个 Set,然后散布到一个新的 Array 中。

How could I Set to acomplish this purpose?我怎样才能完成这个目的? For instance, I have the following code:例如,我有以下代码:

const tmp1=[];
const tmp2=[{
    guid:"e695d848-7188-4741-9c95-44bec634940f",
    name: "Spreading.pdf",
    code: "G1"
  }];
const tmp = [...new Set([...tmp1],[...tmp2])]; //This should remove duplicates, but gets empty array
const x = [...tmp1, ...tmp2]; // This would keep duplicates

The issue is that because tmp1 is an empty array, then I am getting empty result.问题是因为 tmp1 是一个空数组,所以我得到的结果是空的。 However, if I do the following, then getting correct result:但是,如果我执行以下操作,则会得到正确的结果:

const tmp = [...new Set(...tmp1,[...tmp2])];

I think something is missing in here.我认为这里缺少一些东西。

This is an example of duplicated entries where Set is working like a charm just keeping one record:这是一个重复条目的例子,其中 Set 就像一个魅力一样工作,只保留一条记录:

const tmp1=[{
    guid:"e695d848-7188-4741-9c95-44bec634940f",
    name: "Spreading.pdf",
    code: "G1"
  }];
const tmp2=[{
    guid:"e695d848-7188-4741-9c95-44bec634940f",
    name: "Spreading.pdf",
    code: "G1"
  }];
const tmp = [...new Set([...tmp1],[...tmp2])];

This was the original idea, but how about if one of the lists is empty.这是最初的想法,但是如果其中一个列表是空的呢? Then, I am getting an empty array if this occurs.然后,如果发生这种情况,我将得到一个空数组。

Thank you谢谢

Set should take 1 argument but it's taking 2, merge them into one: Set 应该带 1 个参数,但它带 2 个参数,将它们合并为一个:

const tmp = [...new Set([...tmp1, ...tmp2])]; 

Note: that this method won't remove duplicates because you are passing an object to the set and not a reference for it, in this case, you can do this instead:注意:此方法不会删除重复项,因为您将 object 传递给集合而不是它的引用,在这种情况下,您可以改为这样做:

const tmp1 = [];
const tmp2 = [{
  guid: "e695d848-7188-4741-9c95-44bec634940f",
  name: "Spreading.pdf",
  code: "G1"
},
{
  guid: "e695d848-7188-4741-9c95-44bec634940f",
  name: "Spreading.pdf",
  code: "G1"
}
];

// pass a special unique key that differs object from each other to item, in this case i passed guid
const tmp = [...new Map([...tmp1, ...tmp2].map(item => [item['guid'], item])).values()]

Just to explain why this example in the question seemingly work:只是为了解释为什么问题中的这个例子看起来有效:

const tmp1=[{
    guid:"e695d848-7188-4741-9c95-44bec634940f",
    name: "Spreading.pdf",
    code: "G1"
  }];
const tmp2=[{
    guid:"e695d848-7188-4741-9c95-44bec634940f",
    name: "Spreading.pdf",
    code: "G1"
  }];
const tmp = [...new Set([...tmp1],[...tmp2])];

It results in one object because Set() only take one iterable object, so it takes the first one.结果是一个 object,因为Set()只接受一个可迭代的 object,所以它接受第一个。

If const tmp2=[1, 2, 3] in the above example, tmp will still result in the one object in tmp1 .如果const tmp2=[1, 2, 3]在上面的例子中, tmp仍然会导致tmp1中的那个 object 。

More about Set() 更多关于Set()

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

相关问题 在第3方函数javascript中将arrray元素传播到参数中 - Spreading arrray elements into parameters in 3rd party function javascript Javascript - Permutations删除重复元素 - Javascript - Permutations remove the duplicates elements 在 javascript 中查找具有重复项的数组中的第二大元素 - Find second largest elements in array with duplicates in javascript 格式化Json,删除重复项并在javascript中计算元素 - Format Json, remove duplicates and count elements in javascript 在 Javascript 中传播时替换 object 中的密钥 - Replace a key in an object while spreading in Javascript JavaScript - 查找所有具有类的元素,查找重复项并显示它们 - JavaScript - find all elements with class, find duplicates and show them 使用另一个包含重复项javascript的数组从数组中删除元素 - Remove elements from array with another array containing duplicates javascript 如何在 Javascript 中找到 2 个 arrays 中的共同元素而没有重复(除非有 2 个相同的元素)? - How to find the common elements in 2 arrays in Javascript with no duplicates (unless there are 2 of the same element)? CSS / JS:将元素均匀分布在多行中 - CSS/JS: Evenly spreading elements across multiple lines 元素水平相互粘附而不是垂直传播(Nextjs + Tailwind) - Elements stick to each other horizontally instead of spreading verticlly (Nextjs + Tailwind)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM