I have just started learning node.js. I am trying to create a script that will get a list from a file. The list is built like this
Bob:110
Mike:120
Bob:334
Dan:240
And will give this output
name value
It is supposed to sum the numbers associated with the same name.
So in my example it would print
Bob 444
Mike 120
Dan 240
I used the help I found in this site to create some basic script
fs = require('fs');
var array = fs.readFileSync('C:\\data.txt').toString().split("\n");
var items = {}, base, key;
for (var i = 0; i < array.length; i++) {
base = array[i];
key = base[0];
if (!items[key]) {
items[key] = 0;
}
items[key] += base[1];
}
var outputArr = [], temp;
for (key in items) {
temp = [key, items[key]]
outputArr.push(temp);
}
console.log(outputArr);
Notice that I get the list from a text file, but it currently contains what I have posted in this question. The output that I get is very gibberishy, and I am not sure why. This is my output:
[ [ 'B', '0oo' ],
[ 'M', '0i' ],
[ 'D', '0a' ],
[ 'undefined', NaN ] ]
Can anyone please help me understand why this does not work properly and show me how to fix it?
By the way, I am not certain if this is the correct thought line for accomplishing such a task in node.js. I would be happy to get feedback!
Thanks a lot!