14

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.

Kahlil Lechelt
  • 576
  • 2
  • 11
tracer
  • 422
  • 2
  • 7
  • 15
  • This thread: https://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 08 '18 at 08:25
  • Maybe helpful - https://stackoverflow.com/a/69815213/11302100 – Nijat Aliyev Nov 02 '21 at 18:40

5 Answers5

15

You could map back to array of objects using find method, this will return first object with that 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.

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)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
5

You could use a Set and filter by unkown 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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
4

You could create a Map by id and then extract 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()
])
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
  • when i tried this it is throwing "Argument of type 'any[][]' is not assignable to parameter of type 'Iterable<[{}, {}]>'" error – tracer Aug 08 '18 at 08:53
2

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)
Shishir Arora
  • 5,521
  • 4
  • 30
  • 35
0

Using lodash utility library. you can achieve this. let result = _.uniqBy(test, 'id');

Suresh Kumar Ariya
  • 9,516
  • 1
  • 18
  • 27