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?
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);
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'));
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