Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
How to return an array of unique objects based on the id of the object in es6?
    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)]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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()];
about 4 years ago · Juan Pablo Isaza Denunciar

0

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}

about 4 years ago · Juan Pablo Isaza Denunciar

0

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))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda