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});
referhere
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'));
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.