0

I have 2 tsv files (tec00104.tsv & tec00114.tsv), with information about countries. I want to make one single array with the common countries from both tsv files. And the values that comes along. This doesn't seem to work, why?

// initialize array  
var countries = [];
for(var i = 0; i < "tec00114.tsv".length; i++)
{
for(var j = 0; j < "tec00104.tsv".length; j++)

    {
    // if the country code is found in both tsv files
        if("tec00114.tsv"[i][2] === "tec00104.tsv"[j][3]);
        {
        // append new value to the array
            countries.push("tec00114.tsv"[i]);
            countries.push("tec00104.tsv"[j]);
        }
    }
 }
console.log(countries);
  • 3
    Merely referencing a file's name as a string will not cause the file to be accessed; JavaScript thinks you're simply talking about the strings themselves. – Pointy Jun 22 '14 at 12:41
  • [Javascript - read local text file](http://stackoverflow.com/questions/14446447/javascript-read-local-text-file) ? – Aprillion Jun 22 '14 at 13:35

1 Answers1

0

The problem, that you are trying to refer to filenames directly from JavaScript as an array. This is impossible in this way. You need to load them before (e.g. you can do it with $.get() function).

Or you can do it with Alasql library.

<script src="alasql.min.js"></script>
<script>
    var countries = alasql('SELECT t1.*, t2.* FROM TSV("tec00114.tsv") t1 \
        JOIN TSV("tec00104.tsv") t2 ON t1.[2] = t2.[3]'); 
</script>

If your TSV file has headers, use "headers:true" option, like below:

SELECT * FROM TSV("tex00114.tsv", {headers:true});
agershun
  • 4,077
  • 38
  • 41