Created
February 22, 2014 14:51
-
-
Save jrgleason/9156032 to your computer and use it in GitHub Desktop.
Parsing tab delimited file with numbers in Javascript
This file contains hidden or 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
callback = function (body, next) { | |
var b = body; | |
var res = b.split("\r\n"); | |
var line1 = res[0].split("\t"); | |
var collection = []; | |
for(var i=1;i<res.length;i++){ | |
var local = res[i].split("\t"); | |
var object = new Object(); | |
for(var i2=0;i2<line1.length;i2++){ | |
var test = local[i2]; | |
if(! (typeof test === 'undefined')){ | |
test = test.trim(); | |
}; | |
var parsed = parseFloat(test); | |
if(!isNaN(parsed)){ | |
object[line1[i2]]=parsed; | |
} | |
object[line1[i2]]=test; | |
} | |
miao.putKeyValue({ | |
collection: 'SoldHouse', | |
key: uuid.v4(), | |
data: object | |
},callback); | |
collection.push(object); | |
} | |
this.req.collection = collection; | |
return next(); | |
} |
Not sure that is the problem it works fine as is but doesn't make the number into a float it stays a string.
nm I see now
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At line 18, you always assign the value of
test
toobject[line1[i2]]
, regardless of whetherparsed
is or isn’tNaN
. 18 should probably be an else statement.