简体   繁体   中英

using Javascript and node csv-parse to parse csv file into an array

I have a project where I have to process an input CSV file and store it into an array that I can add to, then print it out into a CSV file. I then use the transaction data for the rest of my project so being able to complete this part is vital as testing will be performed with other CSV files.

My issue is that whilst using csv-parse if I use console.table(results); it shows the csv objects when I run the.js file in my command terminal so I know its parsing but no matter what I do I cannot get the objects to go into my array variable.

console.table(results);

Please can someone give me a hint as to where I've gone wrong:

var fs = require('fs');
var parse = require('csv-parse');


var transactionValues = []; //Need an array to hold transactions

//constuctor for transactions
function addData (id, accountType, initiatorType, dateTime, transactions) {
  var data = {
    "AccountID" : id,
    "AccountType" : accountType,
    "InitiatorType" : initiatorType,
    "DateTime" : dateTime,
    "TransactionValues" : transactions
  }
  transactionValues.push(data); //should add a new line
}

    var parser = parse({columns: true}, function (err, results) {
console.table(results);
addData(results.index[0].AccountID, results.index[0].AccountType, results.index[0].InitiatorType, results.index[0].DateTime, results.index[0].TransactionValue, 0);
}); //attempted to save the objects into the array but no success

fs.createReadStream(__dirname+'/testData/customer-1234567-ledger.csv').pipe(parser)

console.log(transactionValues); // array is empty

I believe results is already a normal array as it comes back from csv-parse. You are trying to access the first element with results.index[0] , but it would just be results[0] . Another issue is that fs.createReadStream(...).pipe(...) is asynchronous. That means your console.log will run before it is done parsing. You would need to put any code that has to run after parsing in the callback of your parse function. Something like this:

var parser = parse({columns: true}, function (err, results) {
  console.table(results);
  for (const row of results) { //loop through each object parsed from the csv
    addData(row.AccountID, row.AccountType, row.InitiatorType, row.DateTime, row.TransactionValue, 0);
  }
  console.log(transactionValues); // this should be populated properly

  /* Do anything that needs to use transactionValues here */
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM