1

Im working with a lot of datas which i turned into arrays , for simplicity lets assume i have array that looks like this

["dataone:dataone","datatwo:datatwo","datathree:datathree"]

im writting output to the file using fs.writeFile

but the output is always in the same row e.g dataone:dataone","datatwo:datatwo","datathree:datathree

i would like to output to be like with "\n" e.g

dataone:dataone
datatwo:datatwo
datathree:datathree

is it possible to make output in file look like this? im writting in into .txt file

Darlyn
  • 4,715
  • 12
  • 40
  • 90

2 Answers2

1

Join the data with line breaks before writing to file

var os = require('os');
var brk = os.platform().substring(0,3).toLowerCasee() === 'win'
    ? '\r\n' : '\n';

var data = ["dataone:dataone","datatwo:datatwo","datathree:datathree"]
fs.writeFile(filename, data.join(brk), {encoding : 'utf8'},  function (e) {
   // etc  
});
chriskelly
  • 7,526
  • 3
  • 32
  • 50
1

You can join your array with \n before writing it to the file:

var arr = ["dataone:dataone","datatwo:datatwo","datathree:datathree"]
var arr2 = arr.join('\n');
slomek
  • 4,873
  • 3
  • 17
  • 16