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

210
Visualizações
Unique characters in JavaScript

function unique_char(string) {
  return String.prototype.concat(...new Set(string));
}

console.log(unique_char("Hellooooooooooooo"));

Can anyone help me understand how .concat a new Set will return unique characters ? I thought .concat will add , and why is there a spread operating ... before new Set?

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

0

1)

new Set(string)

As strings in JS are iterable so Set will take string as an input and gives you set of unique characters.

2)

String.prototype.concat(...new Set(string)

The concat() method concatenates the string arguments to the calling string and returns a new string.. One or more strings to concatenate to str. - MDN

So concat will take character by character and append it into a single string

Have a look at the output of the following function

function unique_char(string) {
  const set = new Set(string);
  console.log(...set);

  return String.prototype.concat(...set);
}

console.log(unique_char("Hellooooooooooooo"));

about 4 years ago · Juan Pablo Isaza Relatório

0

So lets cut this command in the single pices:

  1. new Set(string) => this will create a new Set from the string and leave every char only once
  2. the ... is used to "convert" the Set to an array (the set is iterable)
  3. the String.prototype.concat is used to concat all characters from the array that was returned by the spread operator.
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