How to return an array of unique objects based on the id of the object in es6? [duplicate]
Asked Answered
A

5

10
    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)]
Aalst answered 27/12, 2021 at 2:4 Comment(0)
G
10

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}
Gaskell answered 27/12, 2021 at 3:32 Comment(2)
Anyone can explain how this works?Heir
this one does not keep the order of arrayArnst
B
11

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()];
Bregma answered 27/12, 2021 at 2:6 Comment(2)
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.Ezekielezell
same as my answer on here... #70485784Photogenic
G
10

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}
Gaskell answered 27/12, 2021 at 3:32 Comment(2)
Anyone can explain how this works?Heir
this one does not keep the order of arrayArnst
N
7

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))
Nonie answered 28/12, 2021 at 6:55 Comment(1)
This should be the accepted answer, I ran some performance test using console.time() and console.timeEnd() and this method is 133% faster than using const result = Object.values( arr.reduce((acc, obj) => ({ ...acc, [obj.id]: obj }), {}) );Lute
C
3

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'));
Chromium answered 30/3, 2023 at 11:0 Comment(0)
C
0

You can try using pure JavaScript to achieve the function of deleting duplicate items in an 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;
  }

  const map = {};
  const result = [];

  for (const item of array) {
    if (!map[item[key]]) {
      result.push(item);
      map[item[key]] = true;
    }
  }

  return result;
};

console.log( uniqueByKey (array, 'id'));
Cherycherye answered 1/11, 2023 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.