Skip to content
Advertisement

Adding Numbers from csv

I am trying to add all numbers from an column to an variable. The Problem is my code is adding the String to which results into NaN.

var csvData=[];
    let test = 0;
    var parser = parse({delimiter: ','}, function(err, data){
    });

    fs.createReadStream(__dirname+'/test2.csv','utf16le').pipe(parser)
        .on('data', function(csvrow) {
            csvData.push(csvrow);
            test = test + (csvrow[2]);
        })
        .on('end',function() {
            console.log(test)
        });

gives me : “0Daily Device Installs00001000101100” and if I add parseInt(csvrow[2]) I will get NaN for test.

My goal is to add all numbers after Daily Device Installs, what am I missing?

Advertisement

Answer

try

if (!isNaN(csvrow[2])) test += +csvrow[2];
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement