Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!