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

181
Visualizações
How to check for duplicates in a list and save each email one by line in a file

I have a list like this:

[
  'test@t-online.de',
  'kontakt@test.de',
  'info@test.de',
  'test@gmx.de',
  'kontakt@test.de',
]

I want to check for duplicates and save the list in a txt file with one email in each line. The final result would be in this case:

      test@t-online.de
      kontakt@test.de
      info@test.de
      test@gmx.de
 

fs.writeFileSync('./results/test.txt', list)

How to do that?

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

0

You can perform that by performing a loop through the Array if emails and perform a filter to like bellow

let emails = [
  'test@t-online.de',
  'kontakt@test.de',
  'info@test.de',
  'test@gmx.de',
  'kontakt@test.de',
];

let result = emails.reduce((accumulator, current) => {
  if(accumulator.indexOf(current) ===  -1){
    accumulator = accumulator.concat(current);
  }
  return accumulator;
}, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Replace list with [...(new Set(list))].join('\n')

  • new Set() ensures only duplicates are removed and only unique elements remain.
  • join('\n') transforms the Array into a string separated by new-line (\n) character.

const list = [
  'test@t-online.de',
  'kontakt@test.de',
  'info@test.de',
  'test@gmx.de',
  'kontakt@test.de',
];
console.log([...(new Set(list))].join('\n'));

about 4 years ago · Juan Pablo Isaza Relatório

0

for removing duplicated emails:

arr = [...new Set(arr)];

for writing

var writeStream = fs.createWriteStream('./results/test.txt');
writeStream.on('error', function(e) { 
  /* handel error here */ 
});
arr.forEach(email => writeStream.write(email + '\n'));
writeStream.end();

To append data to an existed file Create write stream in append mode

var writeStream = fs.createWriteStream('./results/test.txt', { 'flags': 'a', 'encoding': null, 'mode': 0666});

refer here

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