csv javascript code example
Example 1: Working with CSV in JavaScript
const fs = require("fs");
const csv = require("csvtojson");
const { Parser } = require("json2csv");
(async () => {
const cars = await csv().fromFile("cars.csv");
console.log(cars);
cars[0].Year = 1998;
const carsInCsv = new Parser({ fields: ["Year", "Make", "Model", "Length"] }).parse(cars);
fs.writeFileSync("cars.csv", carsInCsv);
})();
Example 2: parse csv javascript
const csv = require('csv-parser')
const fs = require('fs')
const results = [];
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => console.log(results));