Quantcast
Channel: How to add new column to csv file using node js - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to add new column to csv file using node js

$
0
0

I already tried this using node.js npm package fast-csv, but I don't get a solution, I can read csv file successfully, now I need to add a new column to my existing csv file.

My questions:

How to add new column to csv file? How to update csv?

var csv = require("fast-csv");var fs = require('fs');var stream = fs.createReadStream("file1.csv");var service = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=53.78943,-0.9985&destinations=53.540867,-0.510699&mode=driving&language=en-US';var source = [];var dest = [];var distance = require('google-distance');distance.apiKey = '************';var i = 1;csv    .fromStream(stream, { headers: true })    .on("data", function(data) {        //get source and distance array        source = data.SourceLatLong;       dest = data.DestBREPLatLong;        //print source and destinatoon        console.log(source);        console.log(dest);        distance.get({                // index: i,                origin: source,                destination: dest,                units: 'imperial'            },            function(err, map_data) {                if (err) return console.log(err);                //console.log(map_data);                //console miles of aff                console.log('source lat long '+':'+ data.SourceLatLong +' , '+'Dest lat long'+':'+ data.DestBREPLatLong +','+' distance '+':'+ map_data.distance +''+ i++);            });    }).on("end", function() {    console.log("done");});

In the above program I use the filecsv file1.csv ,from i take two columns SourceLatLong and DestLatLong and I calculate distance in miles. Now I need to add new miles columns to my file .csv


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images