简体   繁体   English

如何使用spread运算符从对象数组中删除重复项

[英]How to remove duplicates from an object array using spread operator

I have below object array with id as unique key": 我有下面的对象数组,id为唯一键“:

var test = [
  {id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
  {id: 2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},
  {id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
  {id: 3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}
]

From this I want to retrieve unique objects using spread operator, I have tried using below code: 从这里我想使用spread运算符检索唯一对象,我尝试使用下面的代码:

const uniKeys = [...(new Set(test.map(({ id }) => id)))];

I am able to retrieve id's only, how can I retrieve the unique objects using spread operator. 我只能检索id,如何使用spread运算符检索唯一对象。 Also, any new ES6 features implementation would be helpful. 此外,任何新的ES6功能实现都会有所帮助。

You could map back to array of objects using find method, this will return first object with that id. 您可以使用find方法map回对象数组,这将返回具有该id的第一个对象。

 var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}] var uniq = [...new Set(test.map(({id}) => id))].map(e => test.find(({id}) => id == e)); console.log(uniq) 

You could also use filter method instead. 您也可以使用filter方法。

 var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}] var uniq = test.filter(function({id}) { return !this[id] && (this[id] = id) }, {}) console.log(uniq) 

You could use a Set and filter by unkown id . 您可以使用Set和过滤未知id

 var test = [{ id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: "" }, { id: 2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode: "" }, { id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: "" }, { id: 3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: "" }], unique = test.filter((s => ({ id }) => !s.has(id) && s.add(id))(new Set)); console.log(unique); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You could create a Map by id and then extract values. 您可以按ID创建Map,然后提取值。 [...new Map(test.map(item => [item.id, item])).values()]

 var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""}, {id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""}, {id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""}, {id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}] console.log([ ...new Map(test.map(item => [item.id, item])).values() ]) 

 var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""}, {id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""}, {id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""}, {id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}]; var uniqArray = Array.from(new Map(test.map(e=>[e.id, e])).values()); console.log(uniqArray) 

Using lodash utility library. 使用lodash实用程序库。 you can achieve this. 你可以做到这一点。 let result = _.uniqBy(test, 'id'); let result = _.uniqBy(test,'id');

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

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