I have a sample array of structs like this
var array =
[ {item1: 'john', item2: 'micheal'},
{item1: 'joe', item2: 'mich'},
{item1: 'jo', item2: 'mi'},
]
I try to parse all these objects (900 objects in this array) into another file using this code
const fs = require('fs');
const writeStream = fs.createWriteStream('file.txt');
const pathName = writeStream.path;
// write each value of the array on the file breaking line
array.forEach(value => writeStream.write(`${value}\n`));
// the finish event is emitted when all data has been flushed from the stream
writeStream.on('finish', () => {
console.log(`wrote all the array data to file ${pathName}`);
});
// handle the errors on the write process
writeStream.on('error', (err) => {
console.error(`There is an error writing the file ${pathName} => ${err}`)
});
// close the stream
writeStream.end();
However when open the file I get
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
which is not what I want. I want to see
{item1: 'john', item2: 'micheal'},
{item1: 'joe', item2: 'mich'},
{item1: 'jo', item2: 'mi'},
which is not what I see. How can I correct this?