How to remove duplicates from an object array using spread operator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to remove duplicates from an object array using spread operator



I have below object array with id as unique key":


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:


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. Also, any new ES6 features implementation would be helpful.





This thread: gist.github.com/telekosmos/3b62a31a5c43f40849bb has a bunch of ways to get unique values from an array, including for an array of objects (i.e. filtering array of objects on unique values of a certain key).
– Jayce444
Aug 8 at 8:25





5 Answers
5



You could map back to array of objects using find method, this will return first object with that id.


map


find




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)





when i tried first solution it says "Type 'Set<any>' is not an array type. Use compiler option '--downlevelIteration' to allow iterating of iterators."
– tracer
Aug 8 at 8:49





@tracer Try Array.from instead of [...]
– Nenad Vracar
Aug 8 at 8:55


Array.from


[...]



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. [...new Map(test.map(item => [item.id, item])).values()]


[...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()
])





when i tried this it is throwing "Argument of type 'any' is not assignable to parameter of type 'Iterable<[, ]>'" error
– tracer
Aug 8 at 8:53




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. you can achieve this.
let result = _.uniqBy(test, 'id');






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS