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

212
Vistas
How to map the items from a list of records contained in an object to a unique array

I have a list of objects that contain an array as one of the values like so:

const obj_arr = [
{'key': ['value-1', 'value-2', 'value-3', 'value-2', 'value-3']},
{'key': ['value-3', 'value-1', 'value-2', 'value-1', 'value-3']},
{'key': ['value-2', 'value-1', 'value-4', 'value-3', 'value-5']}
];

How do I take this list of values and map only the unique items to an array?

so far I have been able to do this:

const obj_arr = [{
    'key': ['value-1', 'value-2', 'value-3', 'value-2', 'value-3']
  },
  {
    'key': ['value-3', 'value-1', 'value-2', 'value-1', 'value-3']
  },
  {
    'key': ['value-2', 'value-1', 'value-4', 'value-3', 'value-5']
  }
];

let unique_arr = [...new Set(obj_arr.map(record => record['key']))];

console.log(unique_arr);

/*
let unique_arr2 = [...new Set(obj_arr.flatMap(record => record['key']))];
console.log('using guidance from Barmar: ', unique_arr2);
*/
.as-console-wrapper { max-height: 100% !important; top: 0 }

but this only gives me back each unique array like this:

unique_arr = [
['value-1', 'value-2', 'value-3', 'value-2', 'value-3'],
['value-3', 'value-1', 'value-2', 'value-1', 'value-3'],
['value-2', 'value-1', 'value-4', 'value-3', 'value-5']
]

what I actually need is this:

unique_arr = ['value-1', 'value-2', 'value-3', 'value-4', 'value-5']
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I don't know if this is the result you're looking for here is a solution with reduce

let uniqueArrReduce = [...new Set(obj_arr.reduce((acc, el) => [...acc, ...el.key], []) )]

with map and flat

let uniqueArrMapFlat = [...new Set(obj_arr.map(el => el.key).flat())]
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could map the values of the objects and get a flat array for the constructor.

const
    array = [{ key: ['value-1', 'value-2', 'value-3', 'value-2', 'value-3'] }, { key: ['value-3', 'value-1', 'value-2', 'value-1', 'value-3'] }, { key: ['value-2', 'value-1', 'value-4', 'value-3', 'value-5'] }],
    unique = [...new Set(array.map(Object.values).flat(Infinity))];

console.log(unique);

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