Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract information in a TSV file and save it in an array in JavaScript ?

I'm running into a problem. If I have only tsv's file name. How can i extract all its information and save it in an array, say X, where each row in the tsv file is represented by an array, say y, where X is an array of ys.

ALso how can i do this if i don't know the headers of columns names ?

like image 851
Fawaz Alazemi Avatar asked Oct 21 '22 11:10

Fawaz Alazemi


1 Answers

You're going to need to use AJAX or just pure XMLHttpRequest.

http://d3js.org/ has a built-in tsv reader that does basically exactly what you want.

The syntax is

d3.tsv("file.tsv", function(data) {
    // use data here
});

The data variable is an array of objects with key/value pairs where the first row in the file is read in as the keys. So, for example

Number Awesomeness
1      5
2      3

will return [{"Number":1, "Awesomeness":5}, {"Number":2, "Awesomeness":3}].

like image 99
Cezary Wojcik Avatar answered Nov 01 '22 11:11

Cezary Wojcik