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

138
Visualizações
How to transfer MAP.values into Arrays efficiently?

In the example below, you see some noisy but straightforward implementation.

Initial Situation

We have an initial Array with repeating information.

const listArray = [
  { songBy: 'George Michael',  uid: 'A',  whatEver: 12},
  { songBy: 'George Michael',  uid: 'A',  whatEver: 13},
  { songBy: 'George Michael',  uid: 'A',  whatEver: 14},
  { songBy: 'Michael Jackson', uid: 'B',  whatEver: 12},
  { songBy: 'Michael Jackson', uid: 'B',  whatEver: 16},
]

STEP 1

We create a new Map because we distinctly want to save artist names, which means → no artist-name-repetitions (and we also want to get rid of the third column, by the way).

We need to use songBy as key, for some reason. We change it within the value into name.

const listMap = new Map();

listArray.forEach(
  row => listMap.set(
   row.songBy, {name: row.songBy, uid: row.uid}
  )
);

STEP 2

Finally, we need an array with its values:

const distinctListArray: Array<any> = [];

listMap.forEach(value => distinctListArray.push(value));

So we achieve a result as an Array of distinct objects in the form of:

[
  name: string
  uid: string
]

Question:

To my mind, this implementation is too noisy and not so elegant. There are too many steps and too many variables. (This example here is a simplified version of a real code I cannot share).

Is there a way to simplify that code and make it more efficient?

EDIT: See online: TypeScript Playground

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Convert the list to [key, value] pairs using Array.map(), and then create the map from the list. Convert the Map back to an array by applying Array.from() to the Map.values() iterator (TS playground):

const listArray = [{"songBy":"George Michael","uid":"A","whatEver":12},{"songBy":"George Michael","uid":"A","whatEver":13},{"songBy":"George Michael","uid":"A","whatEver":14},{"songBy":"Michael Jackson","uid":"B","whatEver":12},{"songBy":"Michael Jackson","uid":"B","whatEver":16}]

const distinctListArray = Array.from(
  new Map(listArray.map(({ songBy, uid }) => 
    [songBy, { name: songBy, uid }]
  )).values()
)

console.log(distinctListArray)

over 4 years ago · Santiago Trujillo 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