How to print each element of an array in new line in txt file using javascript? - Stack Overflow

My result set is an array that returns from a function. ['item1','item2','item

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

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

How do I print them as?

item1
item2
item3
item4
item5
item6
item7
item8

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

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

How do I print them as?

item1
item2
item3
item4
item5
item6
item7
item8
Share Improve this question edited Sep 5, 2018 at 17:45 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Sep 5, 2018 at 17:27 Azim ShaikAzim Shaik 1913 gold badges5 silver badges15 bronze badges 4
  • is uniqueMatches your array? – petey Commented Sep 5, 2018 at 17:29
  • 1 Maybe uniqueMatches.join("\n") – epascarello Commented Sep 5, 2018 at 17:29
  • 1 Check out Array.join. Just join the items with \n, and you should be good. – mccambridge Commented Sep 5, 2018 at 17:29
  • have you tried to iterate your uniqueMatches array construct a string where you append "\r\n" to the end of each item? – Pedro Simão Commented Sep 5, 2018 at 17:30
Add a ment  | 

2 Answers 2

Reset to default 3

Try to use the join() to add the new lines :

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:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744638328a4585281.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信