Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda