简体   繁体   English

如何使用javascript在txt文件的新行中打印数组的每个元素?

[英]How to print each element of an array in new line in txt file using javascript?

My result set is an array that returns from a function. 我的结果集是一个从函数返回的数组。

['item1','item2','item3','item4','item5','item6','item7','item8']

I am writing it into a file using 我正在使用将其写入文件

fs.writeFile('out.txt', uniqueMatches, (err)=>{
     if(err) throw err;
          console.log('Extract saved successful');
});

I see the out.txt as 我看到out.txt为

item1, item2, item3, item4, item5, item6, item7,item8

How do I print them as? 如何将它们打印为?

item1
item2
item3
item4
item5
item6
item7
item8

Try to use the join() to add the new lines : 尝试使用join()添加新行:

fs.writeFile('out.txt',
    uniqueMatches.join('\n'),
    function (err) { console.log('Extract saved successful'); }
);

Use .join to add a line break to your data array that is to be written: 使用.join将换行符添加到要写入的数据数组中:

fs.writeFile('out.txt', uniqueMatches.join('\n');, (err)=>{
            if(err) throw err;
            console.log('Extract saved successful');
        });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM