Skip to content

Instantly share code, notes, and snippets.

@jrgleason
Created February 22, 2014 14:51
Show Gist options
  • Save jrgleason/9156032 to your computer and use it in GitHub Desktop.
Save jrgleason/9156032 to your computer and use it in GitHub Desktop.
Parsing tab delimited file with numbers in Javascript
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();
}
@ian-plosker
Copy link

At line 18, you always assign the value of test to object[line1[i2]], regardless of whether parsed is or isn’t NaN. 18 should probably be an else statement.

@jrgleason
Copy link
Author

Not sure that is the problem it works fine as is but doesn't make the number into a float it stays a string.

@jrgleason
Copy link
Author

nm I see now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment