I'm sure making a silly mistake, but can't trace it. Have a simple script to read in a file of the form:
<option value="us">United States</option>
trying to create a new file of the form:
{"value":"us","name":"United States"}
The issue is jsonC.push(c) is not working and the array remains empty. The console.log(c) does print json representation, still the very next line jsonC.push(c) fails to have any effect on jsonC array and it remains empty.
Here is the code:
var fs = require('fs');
var readline = require('readline');
var src = 'country.txt';
var dest = 'country.json';
var re = /.+["](\w{2})["][>]([a-zA-Z ]+)[<][/].+/;
var jsonC = [];
readline.createInterface({
input: fs.createReadStream(src), terminal: false
}).on('line', function(line) {
var match;
if ((match = re.exec(line)) !== null) {
var c = {};
c.value = match[1];
c.name = match[2];
console.log(c); // prints fine
jsonC.push(c); // not adding to the array
}
});
console.log(jsonC); // prints []
console.log(JSON.stringify(jsonC)); // prints []
This is a stand alone node.js v4.2.6 script running on Win 7.