-
-
Save samjaninf/1a3308211c5c4666a7f665fb0eb5a488 to your computer and use it in GitHub Desktop.
Convert tabular text (csv, tsv, etc.) to javascript array
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
var to_array = function(text, delimeter, has_header, header_size) { | |
var lines = text.split('\n'); | |
var columns = has_header ? lines[header_size - 1].split(delimeter) : null; | |
var rows = []; | |
for (var i=header_size; i<lines.length; i++) { | |
var array = lines[i].split(delimeter); | |
var row = {}; | |
for (var j in array) { | |
row[has_header ? columns[j] : j] = array[j] | |
} | |
rows.push(row); | |
} | |
return rows; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment