简体   繁体   English

如何检查列表中的重复项并将每个 email 逐行保存在文件中

[英]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.我想检查重复项并将列表保存在一个 txt 文件中,每行一个 email。 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) fs.writeFileSync('./results/test.txt', 列表)

How to do that?怎么做?

You can perform that by performing a loop through the Array if emails and perform a filter to like bellow您可以通过在 Array if emails 中执行循环来执行此操作,并执行过滤器以喜欢下面

 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')list替换为[...(new Set(list))].join('\n')

  • new Set() ensures only duplicates are removed and only unique elements remain. new Set()确保仅删除重复项并仅保留唯一元素。
  • join('\n') transforms the Array into a string separated by new-line ( \n ) character. join('\n')将 Array 转换为由换行符 ( \n ) 分隔的字符串。

 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将 append 数据写入现有文件在 append 模式下创建写入 stream

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我已将"a"标志用于 append 同一文件中的数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的字符串结果中提取电子邮件地址并将它们保存在一个 txt 文件中,每行一个 - How to extract email addresses from my string result and save them in a txt file one per line 在MVC 4剃须刀中一个接一个地选中列表中的每个复选框后如何调用Ajax函数 - How can I call Ajax function after check each check box in a list one by one in mvc 4 razor 如何修改引导程序注册代码以检查电子邮件重复项? - How to modify bootstrap code for signup to check for email duplicates? 保存用户前如何检查电子邮件是否存在 - How to check if email exist before save the user 如何逐行打开和读取文件(JavaScript)并保存在列表中? - How do I open and read from file line by line (JavaScript) and save in list? 有没有办法检查“下载前询问每个文件的保存位置”是否已激活? - Is there a way to check the “Ask where to save each file before downloading” is activated? 保存后,每行显示文件名的sublime - sublime showing file name on each line after save 如何检查 HTML 集合中的重复项? - How to check for duplicates in an HTMLcollection? 检查销售订单是否重复(客户订单)保存 - Check for Duplicates on sales order(Customer PO) Save 检查 CSV 解析文件中的重复项 - Check duplicates in CSV parsed file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM