6
    Array(96) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
​
0: Object { id: 1, name: "PrimShal01", period: 3, … }
1: Object { id: 61, name: "TertDeep01", period: 1, … }
2: Object { id: 37, name: "SecoDeep01", period: 2, … }
3: Object { id: 49, name: "TertShal01", period: 1, … } ​
4: Object { id: 13, name: "PrimDeep01", period: 3, … }
5: Object { id: 61, name: "TertDeep01", period: 1, … }

When I try the following code I only get the unique id, but I want the objects:

const uniques = [new Set(all_filter_ids.map(pos => pos.id))]

When I try the following code I get the the same as before:

const uniques = [new Set(all_filter_ids)]
ML Rozendale
  • 135
  • 1
  • 1
  • 7

4 Answers4

6

Turn them into a Map indexed by ID (only one object can exist for a key), then turn the Map's values back into the array.

const map = new Map(all_filter_ids.map(pos => [pos.id, pos]));
const uniques = [...map.values()];
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 1
    This will keep the last element by id. If you want the first one, you can add the items in reverse order into the Map `new Map(all_filter_ids.map(...).reverse())` and if order matters, `[...map.values()].reverse()` to get them back in the original order out. – Thomas Dec 27 '21 at 02:14
  • same as my answer on here... https://stackoverflow.com/questions/70485284/how-to-remove-duplicate-elements-from-an-array-of-objects-js/70485379#70485379 – skara9 Dec 27 '21 at 02:36
4

Another one solution:

const arr = [{ id: 1, name: "PrimShal01", period: 3},{ id: 61, name: "TertDeep01", period: 1},{ id: 37, name: "SecoDeep01", period: 2},{ id: 49, name: "TertShal01", period: 1},{ id: 13, name: "PrimDeep01", period: 3},{ id: 61, name: "TertDeep01", period: 1}]

const result = Object.values(
    arr.reduce((acc, obj) => ({ ...acc, [obj.id]: obj }), {})
);

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
2

Maintain a set for tracking id's and use filter

const uniq = (arr, track = new Set()) =>
  arr.filter(({ id }) => (track.has(id) ? false : track.add(id)));

const arr = [
  { id: 1, name: "PrimShal01", period: 3 },
  { id: 61, name: "TertDeep01", period: 1 },
  { id: 37, name: "SecoDeep01", period: 2 },
  { id: 49, name: "TertShal01", period: 1 },
  { id: 13, name: "PrimDeep01", period: 3 },
  { id: 61, name: "TertDeep01", period: 1 },
];

console.log(uniq(arr))
Siva K V
  • 10,561
  • 2
  • 16
  • 29
2

You can use map to get distinct objects from an array. filter(Boolean) is to strip out any null values in the array.

const array = [
  { id: 1, name: "PrimShal01", period: 3},
  { id: 61, name: "TertDeep01", period: 1},
  { id: 37, name: "SecoDeep01", period: 2},
  { id: 49, name: "TertShal01", period: 1},
  { id: 13, name: "PrimDeep01", period: 3},
  { id: 61, name: "TertDeep01", period: 1}
]

const uniqueByKey = (array = [], key = '') => {
  if (!key) {
    return array;
  }

  return [
    ...new Map(
      array
        .filter(Boolean)
        .map((item) => [item[key], item]),
    ).values(),
  ];
};

console.log(uniqueByKey(array, 'id'));
Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44