Created
September 25, 2014 12:42
-
-
Save adriancbo/f87b2547d0f78aa8038a to your computer and use it in GitHub Desktop.
NodeJS Process File then Output to File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
fs.readFile('cats.txt', function(err, data) { | |
if(err) throw err; | |
var array = data.toString().split("\n"); | |
var lookup = array.map(function(line) { | |
var significant = line.split('. ')[1].split(' ('); | |
return { category: significant[0], id: significant[1].split(')')[0] }; | |
}); | |
var json = JSON.stringify(lookup); | |
fs.writeFile('cats.js', json, function (err) { | |
if (err) throw err; | |
console.log('It\'s saved!'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment