简体   繁体   English

消除对象数组中的重复项

[英]Get rid of duplicates in array of objects

I'm trying to get rid of duplicates in array of objects.我试图摆脱对象数组中的重复项。 I want my code linear so I'm using hashtable:我希望我的代码是线性的,所以我使用哈希表:

output = Array.from(new Set(output.map(e => e.company)))
  .map(company => { return output.find(e => e.company === company) })

This code works properly for this array of objects:此代码适用于此对象数组:

let output = [
  { company: 'sony'},
  { company: 'sony'},
  { company: 'apple'}
]

but thing is that I want to reuse this code so I would like to move it to function which would take array output and company string.但问题是我想重用这段代码,所以我想把它移到 function ,这将采用数组outputcompany字符串。 I don't know how to do that, I tried like this:我不知道该怎么做,我试过这样:

const uniq = (array, key) => {
  return Array.from(new Set(array.map( e => e[key] )))
    .map(key => { return array.find(e => e[key] === key) })
}

but this return array of two undefined.但这两个未定义的返回数组。


Also second thing is that it's pretty complicated code for such trivial task.第二件事是,对于这种微不足道的任务,它的代码非常复杂。 Isn't there some easier way to do same without losing performance?难道没有一些更简单的方法可以在不损失性能的情况下做同样的事情吗?

This one-liner works with arbitrary objects, not necessarily key:value pairs:这个单线适用于任意对象,不一定是键:值对:

 let uniqBy = (a, key) => [...new Map(a.map(x => [x[key], x]).reverse()).values()]; // let data = [ {company: 'sony', more: 1}, {company: 'apple', more: 1}, {company: 'sony', more: 2}, {company: 'sony', more: 3}, {company: 'apple', more: 3}, ] console.log(uniqBy(data, 'company'))

If you don't care about the order of the result, you can also skip .reverse() .如果你不关心结果的顺序,你也可以跳过.reverse()

You could map the unique values and create a new object.您可以 map 的唯一值并创建一个新的 object。

 const unique = (array, key) => Array.from(new Set(array.map(o => o[key])), value => ({ [key]: value })); var output = [{ company: 'sony'}, { company: 'sony'}, { company: 'apple'}]; console.log(unique(output, "company"));

You have naming conflicts, just rename the second key :您有命名冲突,只需重命名第二个key

 let input = [{ company: "sony" }, { company: "sony" }, { company: "apple" }]; const uniq = (array, key) => { return Array.from(new Set(array.map(e => e[key]))).map(key1 => { // ^^^^ return array.find(e => e[key] === key1); // ^^^^ }); }; const result = uniq(input, "company"); console.log(result);

I think the issue is you are overwriting the definition of key in the .map() callback.我认为问题是您正在覆盖.map()回调中的key定义。 This version - with a separate variable name for the callback parameter - seems to do what you need:这个版本 - 回调参数有一个单独的变量名 - 似乎可以满足您的需求:

 let output = [ { company: 'sony'}, { company: 'sony'}, { company: 'apple'} ] const uniq = (array, key) => { return Array.from(new Set(array.map( e => e[key] ))).map(x => { return array.find(e => e[key] === x) }) } console.log(uniq(output, "company"));

You need to rename your second map parameter to something other than key , otherwise the key inside find will not be the parameter key that is passed to the function:您需要将第二个map参数重命名为key以外的其他参数,否则 find 中的key将不是传递给 function 的参数key

 let output = [ { company: 'sony'}, { company: 'sony'}, { company: 'apple'} ] const uniq = (array, key) => { return Array.from(new Set(array.map( e => e[key] ))).map(k => { return array.find(e => e[key] === k) }) } console.log(uniq(output, 'company'));

If you are allowed to use Lodash there is a simple way of eliminating duplicates如果您被允许使用 Lodash,则有一种消除重复项的简单方法

_.uniqBy(your_data, 'company');

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

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