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

142
Visualizações
Convert each Array in a typescript object to a set

I am a beginner in Typescript, and I am looking for a neat way to convert all the arrays in an object to a set.

Input:

const inputObject = {'a': [1, 2, 3], 'b': [2, 2, 2], 'c': [3,3,2,4]};

Output

{'a': Set(3){1, 2, 3}, 'b': Set(1){2}, 'c': Set(3){2, 3, 4};

I know how to work on it iteratively key by key and get the solution. I am looking if I can do it more elegantly and modify the same inputObject instead of creating a new object.

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

I think this code Works fine!

const inputObject = {'a': [1, 2, 3], 'b': [2, 2, 2], 'c': [3,3,2,4]};
 for (const key in inputObject ) {
    inputObject[key] = new Set(inputObject[key]);
    }
console.log(inputObject);
about 4 years ago · Santiago Gelvez Relatório

0

This code works, but not sure why it isn't working in StackOverflow's code editor.

You need to do the following

  1. get the keys of the object you are trying to modify
  2. Update each of them as Set.

const inputObject = {'a': [1, 2, 3], 'b': [2, 2, 2], 'c': [3,3,2,4]};
console.log(inputObject);
Object.keys(inputObject).forEach(key => inputObject[key] = new Set(inputObject[key]));
console.log(inputObject);

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