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

106
Views
Filter the unique object by name from array

I am trying to filter unique object from the places Array inside the list Array, but not able to get the exact solution to find the unique place name, please find the array data below=>

"list":[
   {
      "id":1,
      "uID": 1 
      "places":[
         {
            "name":"USA"
         },
         {
            "name":"USA"
         },
         {
            "name":"GER"
         }
      ]
   },
   {
      "id":2,
      "uID":2
      "places":[
         {
            "name":"USA"
         },
         {
            "name":"GER"
         },
         {
            "name":"GER"
         }
      ]
   }
]

The Expected output should be like this =>

"list":[
   {
      "id":1,
      "uID": 1 
      "places":[
         {
            "name":"USA"
         },
         {
            "name":"GER"
         }
      ]
   },
   {
      "id":2,
      "uID":2
      "places":[
         {
            "name":"USA"
         },
         {
            "name":"GER"
         }
      ]
   }
]

Appreciate your help.

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You can use Array.map() to map to map each member of the list to a new value.

For each value, we can remove duplicate places using a Set object.

We instantiate the Set using an array of the place names, using .map() again. The Set will then contain a list of the unique place names.

We then assign this to the places property of each output item.

const list = [ { "id":1, "uID": 1, "places":[ { "name":"USA" }, { "name":"USA" }, { "name":"GER" } ] }, { "id":2, "uID":2, "places":[ { "name":"USA" }, { "name":"GER" }, { "name":"GER" } ] } ]

const result = list.map(({ places, ...obj }) => {
    const uniquePlaces = new Set(places.map(item => item.name));
    return { ...obj, places: [ ...uniquePlaces].map(name => ({ name })) }
})

console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Santiago Gelvez Report

0

You can use Set() to filter duplicate by using add() method. for more reference take a look at this link : https://levelup.gitconnected.com/how-to-find-unique-values-by-property-in-an-array-of-objects-in-javascript-50ca23db8ccc

about 4 years ago · Santiago Gelvez Report

0

  1. Convert the object(s) in places array as (JSON) string via JSON.stringify().
  2. With new Set() to distinct the JSON string.
  3. Convert to array from result 2 via Array.from().
  4. Convert each (JSON) string in the array (from 3) to the object via JSON.parse().
list = list.map(x => {
    x.places = Array.from([...new Set(x.places.map(x => JSON.stringify(x)))])
        .map(x => JSON.parse(x));

    return x;
})

Sample Typescript Playground

about 4 years ago · Santiago Gelvez 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!