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.
JavaScript
x
14
14
1
var csvData=[];
2
let test = 0;
3
var parser = parse({delimiter: ','}, function(err, data){
4
});
5
6
fs.createReadStream(__dirname+'/test2.csv','utf16le').pipe(parser)
7
.on('data', function(csvrow) {
8
csvData.push(csvrow);
9
test = test + (csvrow[2]);
10
})
11
.on('end',function() {
12
console.log(test)
13
});
14
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
JavaScript
1
2
1
if (!isNaN(csvrow[2])) test += +csvrow[2];
2