2

I have to take the first line from my text file and compare to the values from my array. I'm using new FileReader() to get the first line from my .txt file. Here is my code:

var fileExist = $('#fileUpload')[0];
var reader = new FileReader();
var file = fileExist.files[0];
reader.onload = function(e) {
    var text = reader.result;                 
    var firstLine = text.split('\n').shift(); 
    var columnNames = firstLine.split('\t');

    console.log(columnNames);
}
reader.readAsText(file, 'UTF-8');

Here is my output:

["LAST_NAME", "FIRST_NAME", "BIRTH_DATE", "GENDER",  "PHONE", "EMAIL\r"]

For some reason my last element in the array has \r. This is the case with any text file that i try to load with this function. How I can get rid of \r ? I tried to include in my split function \r but that did not work, first it looks that all comma's were removed and there is no [] brackets around the text. If anyone can help with this problem please let me know. My text file is tab delimited.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • 2
    `text.split(/\r\n|\n/)...` – Teemu Oct 10 '16 at 16:29
  • @Teemu I tried and works fine with the code that you provided. What is a difference and what is actually \r ? Thank you! – espresso_coffee Oct 10 '16 at 16:32
  • 1
    http://stackoverflow.com/questions/1761051/difference-between-n-and-r – Ali Somay Oct 10 '16 at 16:34
  • Is there a way how I can check everything but the first line from my .txt file? Do I have to use .slice() for that or something else? – espresso_coffee Oct 10 '16 at 19:35
  • Like so: `text=reader.result.split(/\r\n|\n/); firstLine=text.shift();` This way the first line is pulled into its own variable, and the rest of the content will be in the `text` array. – Teemu Oct 11 '16 at 17:24

0 Answers0