Here, I am fetching a data from the user. The first line contains an integer, denoting the number of entries in the phone book. Each of the subsequent lines describes an entry in the form of space-separated values on a single line. The first value is a friend's name, and the second value is a digit phone number.
For example:
2
sam 99912222
tom 11122222
So, while processing this input, I am taking the number of entries from the user and assigning it to variable noOfIterations. For the subsequent entries, I am assigning it to a list of objects wherein the format will be : for eg. [{ sam: '99912222' },{ tom: '11122222' }].
But the problem is I get [ 'sam 99912222', 'tom 11122222' ] instead while consoling the map variable. Why do I get this?
function processData(input) {
var noOfIterations = 0;
var map = input.split('\n').filter(function (string, index){
if(index===0){
noOfIterations = parseInt(string);
}
else{
var splitStrings = string.trim().split(' ');
var obj = {};
obj[splitStrings[0]] = splitStrings[1];
console.log(obj);
return obj;
}
});
console.log(map);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});