Javascript代码返回undefined:南在执行输出?

我是一个JavaScript的新手,并试图学习一些基本知识。 所以我写了一个小文字阅读的脚本。 以下是我的代码。

// Load the fs (filesystem) module var fs=require('fs'); // Read the contents of the file into memory. fs.readFile('example.txt', function (err, logData) { // If an error occurred, throwing it will //display the exception and end our app. if(err) throw err; // logData is a Buffer, convert to string. var text = logData.toString(); var results = {}; var lines = text.split('\n'); lines.forEach(function(line){ var parts = line.split(' '); var letter = parts[1]; var count = parseInt(parts[2]); if (!results[letter]){ results[letter] = 0; } results[letter] +=parseInt(count); }); console.log(results); }); 

我的input文件是

 2013-08-09T13:50:33.166ZA 2 2013-08-09T13:51:33.166ZB 1 2013-08-09T13:52:33.166ZC 6 2013-08-09T13:53:33.166ZB 8 2013-08-09T13:54:33.166ZB 5 

example.txt中的行数是5

 wc -l example.txt 5 example.txt 

使用Node.js执行上面的代码导致

 node my_parser.js [ '2013-08-09T13:50:33.166ZA 2', '2013-08-09T13:51:33.166ZB 1', '2013-08-09T13:52:33.166ZC 6', '2013-08-09T13:53:33.166ZB 8', '2013-08-09T13:54:33.166ZB 5', '' ] { A: 2, B: 14, C: 6, undefined: NaN } 

如果example.txt文件中只有五行? 为什么一个额外的空string被添加到列表中? 这是预期从splitfunction? 请帮忙

文件最后是否有换行符? 如果是这样,最后一个数组项将是一个空string