It treats each element in the array as an array, so '456' is treated as an array with the elements 4,5,6.
A strange workaround I did was to loop through the array and put each element inside its own array, so it would be:
var newArray = [];
for( var i = 0; i < tempArray.length; i++){
var arr = [];
arr.push(tempArray[i]);
newArray.push(arr);
}
And then write the "newArray".
But otherwise if you write row by row, you can put each element inside a bracket, like:
var fast_csv = fastcsv.createWriteStream();
var writeStream = fs.createWriteStream("outputfile.csv");
fast_csv.pipe(writeStream);
for(var i = 0; i < tempArray.length; i++){
fast_csv.write( [ tempArray[i] ] ); //each element inside bracket
}
fast_csv.end();
Answer from Galivan on Stack Overflow
» npm install fast-csv
Read and Write CSV using fast-csv package in Node.js - Stack Overflow
FastCSV: 5X faster than encoding/csv
Reading from file with fast-csv and writing into an array
Show HN: uDSV.js – A faster CSV parser
Videos
It treats each element in the array as an array, so '456' is treated as an array with the elements 4,5,6.
A strange workaround I did was to loop through the array and put each element inside its own array, so it would be:
var newArray = [];
for( var i = 0; i < tempArray.length; i++){
var arr = [];
arr.push(tempArray[i]);
newArray.push(arr);
}
And then write the "newArray".
But otherwise if you write row by row, you can put each element inside a bracket, like:
var fast_csv = fastcsv.createWriteStream();
var writeStream = fs.createWriteStream("outputfile.csv");
fast_csv.pipe(writeStream);
for(var i = 0; i < tempArray.length; i++){
fast_csv.write( [ tempArray[i] ] ); //each element inside bracket
}
fast_csv.end();
Change: tempArray.push(data[1]); to: tempArray.push([data[1]]);
writeToPath takes an array of arrays.
Docs: https://www.npmjs.com/package/fast-csv
Well, I know that this question has been asked a long back but just now I got to work with CSV file for creating API with node js. Being a typical programmer I googled "Reading from a file with fast-csv and writing into an array" well something like this but till date, there isn't any proper response for the question hence I decided to answer this.
Well on is async function and hence execution will be paused in main flow and will be resumed only after nonasync function gets executed.
var queryParameter = ()=> new Promise( resolve =>{
let returnLit = []
csv.fromPath("<fileName>", {headers : true})
.on('data',(data)=>{
returnLit.push(data[<header name>].trim())
})
.on('end',()=>{
resolve(returnLit)
})
})
var mainList = [];
queryParameter().then((res)=>mainList = res)
If you want to validate something pass argument into queryParameter() and uses the argument in validate method.
The "on data" callback is asynchronous, and the commands that follow the callback will run before the callback finishes. This is why the code does not work, and this reasoning has been pointed out by others who have posted answers and comments.
As for a good way to accomplish the task, I have found that using the "on end" callback is a good fit; since the intention here is to "do something" with the whole data, after the file has been read completely.
var dataArr = [];
csv.fromPath("datas.csv", {headers: true})
.on("data", data => {
dataArr.push(data);
})
.on("end", () => {
console.log(dataArr.length);
// > 4187
});
» npm install @fast-csv/parse
» npm install @fast-csv/format