Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  130] [ 2]  / answers: 1 / hits: 164271  / 11 Years ago, fri, july 12, 2013, 12:00:00

I have a sample array as follows



var arr = [ [ 1373628934214, 3 ],
[ 1373628934218, 3 ],
[ 1373628934220, 1 ],
[ 1373628934230, 1 ],
[ 1373628934234, 0 ],
[ 1373628934237, -1 ],
[ 1373628934242, 0 ],
[ 1373628934246, -1 ],
[ 1373628934251, 0 ],
[ 1373628934266, 11 ] ]


I would like to write this array to a file such as I get a file as follows



1373628934214, 3 
1373628934218, 3
1373628934220, 1
......
......

More From » node.js

 Answers
23

If it's a huuge array and it would take too much memory to serialize it to a string before writing, you can use streams:



var fs = require('fs');

var file = fs.createWriteStream('array.txt');
file.on('error', function(err) { /* error handling */ });
arr.forEach(function(v) { file.write(v.join(', ') + 'n'); });
file.end();

[#77037] Thursday, July 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elvisissacg

Total Points: 410
Total Questions: 108
Total Answers: 121

Location: Monaco
Member since Tue, Jun 16, 2020
4 Years ago
;