Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Created April 8, 2014 23:35
Show Gist options
  • Save raineorshine/10209074 to your computer and use it in GitHub Desktop.
Save raineorshine/10209074 to your computer and use it in GitHub Desktop.
Rough example of parsing a pipe-delimited dataset.
var dataset = "...";
/*
FEATURE_ID|COL2|COL3
400|Ted|25
401|Bob|29
402|Sam|28
*/
// get an array of lines
var lines = dataset.split('\n');
// get the headings
var headings = lines[0];
// this will store an array of objects
var data = [];
// push each row onto the data array
for(var i=1; i<lines.length; i++) {
// get an array of each value in the row
var rowData = lines[i].split('|');
// build an object
var obj = {};
/*
{
FEATURE_ID: 405
}
*/
for(var j=0; j<headings.length; j++) {
obj[headings[i]] = rowData[i];
}
data.push(obj);
}
// data is now a JSON object with all the data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment