3

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
4
  • is uniqueMatches your array? Commented Sep 5, 2018 at 17:29
  • 1
    Maybe uniqueMatches.join("\n") Commented Sep 5, 2018 at 17:29
  • 1
    Check out Array.join. Just join the items with \n, and you should be good. 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? Commented Sep 5, 2018 at 17:30

2 Answers 2

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'); }
);
Sign up to request clarification or add additional context in comments.

3 Comments

May I ask why you called map, uniqueMatches sould already be a array so whats the point?
Thanks for your intevention @dotoconnor I just forgot it there
Glad I could help brother. Happy coding.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.