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?
>Solution :
Complete code. I have used "a" flag to append the data in same file
let fs = require('fs');
let data = [
'test@t-online.de',
'kontakt@test.de',
'info@test.de',
'test@gmx.de',
'kontakt@test.de',
];
let stream = fs.createWriteStream("emails.txt", {'flags': 'a'});
stream.once('open', function(fd) {
// this will remove duplicates from the array
const result = data.filter((item, pos) => data.indexOf(item) === pos)
result.forEach(email => stream.write(email + '\n'));
});