raineorshine
4/8/2014 - 11:35 PM

Rough example of parsing a pipe-delimited dataset.

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