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

Viewed 5845

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
2 Answers

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