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

208
Visualizações
Obtenga valores únicos usando el filtro en JS

Quiero mostrar solo valores uniq cuando estoy usando el filtro

Por ejemplo, tengo esto:

 const test = 'This is Right'; const test1 = 'This is wrong'; const test2 = 'This is Right'; const test3 = 'The word is hello' const result = [test, test1, test2, test3].filter(value => value).join(' - '); console.log(result);

La salida es:

 This is Right - This is wrong - This is Right - The word is hello

Lo que quiero :

 This is Right - This is wrong - The word is hello

Ya veo algunos problemas similares pero no quiero trabajar con matriz. Gracias por tu ayuda

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

0

1) Puede obtener un valor único usando Set

 const test = "This is Right"; const test1 = "This is wrong"; const test2 = "This is Right"; const test3 = "The word is hello"; const set = new Set().add(test).add(test1).add(test2).add(test3); const result = [...set].join(" - "); console.log(result);

2) Usar Array literal con Set

 const test = "This is Right"; const test1 = "This is wrong"; const test2 = "This is Right"; const test3 = "The word is hello"; const set = new Set([test, test1, test2, test3]); const result = [...set].join(" - "); console.log(result);

3) Usar solo con matriz

 const test = "This is Right"; const test1 = "This is wrong"; const test2 = "This is Right"; const test3 = "The word is hello"; const dict = []; const result = [test, test1, test2, test3] .filter((value) => { if (!dict.includes(value)) { dict.push(value); return true; } return false; }) .join(" - "); console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Podría usar Set e inicializarlo con una matriz de cadenas

 const test = 'This is Right'; const test1 = 'This is wrong'; const test2 = 'This is Right'; const test3 = 'The word is hello'; const m = new Set([test, test1, test2, test3]) console.log([...m].join(' - '))
about 4 years ago · Juan Pablo Isaza Relatório

0

Como puede ver, esta es una versión corta de @decpk

 const test = "This is Right"; const test1 = "This is wrong"; const test2 = "This is Right"; const test3 = "The word is hello"; const result = [...new Set([test, test1, test2, test3])].join(" - "); console.log(result);

Como puede ver, esto en realidad usa la matriz [test, test1, test2, test3] crea un Set , eliminará el duplicado y luego usará join para crear una string .


Puedes usar filter como:

 const test = "This is Right"; const test1 = "This is wrong"; const test2 = "This is Right"; const test3 = "The word is hello"; const result = [test, test1, test2, test3].filter((value, index, self) => self.indexOf(value) === index).join(" - "); console.log(result);

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