node.js - how to write an array to file

前端 未结 5 1525
礼貌的吻别
礼貌的吻别 2020-12-13 03:46

I have a sample array as follows

var arr = [ [ 1373628934214, 3 ],
  [ 1373628934218, 3 ],
  [ 1373628934220, 1 ],
  [ 1373628934230, 1 ],
  [ 1373628934234,         


        
5条回答
  •  半阙折子戏
    2020-12-13 04:16

    We can simply write the array data to the filesystem but this will raise one error in which ',' will be appended to the end of the file. To handle this below code can be used:

    var fs = require('fs');
    
    var file = fs.createWriteStream('hello.txt');
    file.on('error', function(err) { Console.log(err) });
    data.forEach(value => file.write(`${value}\r\n`));
    file.end();
    

    \r\n

    is used for the new Line.

    \n

    won't help. Please refer this

提交回复
热议问题