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

223
Vistas
How to get unique values from an array of objects in JavaScript, but filtered by certain value

I would like to extend the question that has been asked some time ago: How to get distinct values from an array of objects in JavaScript?

my case is very similar: I have the following:

var array = 
    [
        {"name":"Doctor", "age":17}, 
        {"name":"Doctor", "age":17},
        {"name":"Doctor", "age":18},
        {"name":"Nurse", "age":17}, 
        {"name":"Nurse", "age": 35}
        {"name":"Nurse", "age": 35}
    ]

What is the best way to be able to get an array of all of the distinct ages, but for chosen speciality such that I get an result array of:

filter by doctor parameter: [17, 18]

filter by nurse parameter: [17, 35]

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Use array functions

const array = [{
    "name": "Doctor",
    "age": 17
  },
  {
    "name": "Doctor",
    "age": 18
  },
  {
    "name": "Nurse",
    "age": 17
  },
  {
    "name": "Nurse",
    "age": 35
  }
]

console.log(getUniqueAges(array, 'Doctor'))
console.log(getUniqueAges(array, 'Nurse'))

function getUniqueAges(data, name) {
  return [...new Set(data
    .filter(datum => datum.name === name)
    .map(datum => datum.age))]
}

about 4 years ago · Santiago Trujillo Denunciar

0

just like in the answer you linked, but you filter (the example below is filtered by name==="doctor) the array before you pass it to the Set:

const data =     [
    {"name":"Doctor", "age":17}, 
    {"name":"Doctor", "age":18},
    {"name":"Nurse", "age":17}, 
    {"name":"Nurse", "age": 35}
];
const unique = [...new Set(data.filter(item => item.name === "doctor").map(item => item.age))]; // [ '17', '18']
about 4 years ago · Santiago Trujillo 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