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

204
Visualizações
Why do I get wrong string format for javascript Set

I want to get test2 as String but I got only "1" instead of "1, 2, 3".

let test = [1, 2, 3];
console.log(String(test));

let test2 = new Set([1, 2, 3]);
console.log(String(...test2));

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

String() converts an array to a string.

You were not converting Set into an array correctly, you just needed square brackets with the spread operator.

let test2 = new Set([1, 2, 3, 3]);
console.log(String([...test2]));

about 4 years ago · Juan Pablo Isaza Relatório

0

String(...test2) spreads out the elements from the set into discrete arguments to String. With your example set, it's the same as doing String(1, 2, 3). The String function doesn't do anything with any arguments you pass it other than the first one, so String(1, 2, 3) gives you the same result as String(1): It converts the argument to string, returning "1".

If you want to get, say, "1, 2, 3", you could spread the contents of the set into an array and use join:

let test2 = new Set([1, 2, 3]);
console.log(
    [...test2].join(", ")
);

about 4 years ago · Juan Pablo Isaza 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