node.js - how to write an array to file
Remember you can access good old ECMAScript APIs, in this case, JSON.stringify()
.
For simple arrays like the one in your example:
require('fs').writeFile(
'./my.json',
JSON.stringify(myArray),
function (err) {
if (err) {
console.error('Crap happens');
}
}
);
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();