16

I'm able to read a file line by line but I do not know how to split each lines using the tab delimited. here my code. Need some help on this issue

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Sum of a Column in JavaScript</title>
    </head>

    <input type="file" name="file" id="file">

    <script type="text/javascript">

    document.getElementById('file').onchange = function(){

    var file = this.files[0];

    var reader = new FileReader();
    reader.onload = function(progressEvent){
    // Entire file
    console.log(this.result);

    // By lines
    var lines = this.result.split('\n');
    for(var line = 0; line < lines.length; line++){
        // By tabs
        var tabs = lines[line].split('\\t');
        for(var tab = 0; tab < tabs.length; tab++){    
                alert(tabs[tab]);
        }   
    }
  };
  reader.readAsText(file);
};

</script>
2
  • 9
    Should be just '\t', not '\\t' - you got it right for '\n'!
    – Pointy
    Mar 8, 2015 at 14:38
  • Thanks, merci beaucoup Mar 8, 2015 at 14:47

3 Answers 3

17

I found this useful, and replaced the for ... loops with the js .map() function. Also, I load data into arrays:

    // By lines
    var arr1 = [];
    var arr2 = [];
    var arr3 = [];
    var arr4 = [];
    var arr5 = []; // assuming 5 tabs
    var lines = this.result.split('\n');
    lines.map(function(item){
      var tabs = item.split('\t');
      console.log("0",tabs[0], "1",tabs[1], "2",tabs[2],"3", tabs[3],"4", tabs[4], "5", tabs[5], "6", tabs[6]);
      arr1.push(tabs[0]);
      arr2.push(tabs[1]);
      arr3.push(tabs[2]);
      arr4.push(tabs[3]);
      arr5.push(tabs[4]);
    });
    // test two of the arrays after reading:
    for (var i = 0; i < mdarr.length; i++) {
      console.log(arr1[i], arr2[i]);
    };
  }
  reader.readAsText(file);
};
0
5

This can be done with a one liner. First split by new line then split by tab. The result is an 2d array where the first item is the header row.

const parsedString = tabbedString.split('\n').map((line) => line.split('\t'))

Example

const tabbedString = `Prefix    Name    Last Name   Email   Phone   Age Role
    Jim Loco    [email protected]        32  Admin
Mrs.    Sara    Foo [email protected]   124389  44  Admin
Mr. John    Deer    [email protected]      37  Developer`

const parsedString = tabbedString.split('\n').map((line) => line.split('\t'))

console.log(parsedString)
[
  [
    "Prefix",
    "Name",
    "Last Name",
    "Email",
    "Phone",
    "Age",
    "Role"
  ],
  [
    "",
    "Jim",
    "Loco",
    "[email protected]",
    "",
    "32",
    "Admin"
  ],
  [
    "Mrs.",
    "Sara",
    "Foo",
    "[email protected]",
    "124389",
    "44",
    "Admin"
  ],
  [
    "Mr.",
    "John",
    "Deer",
    "[email protected]",
    "",
    "37",
    "Developer"
  ]
]

Note that stack overflow replaces tabs with 4 spaces so in the input string you will actually find 4 spaces and not a tab but in my original code they are indeed tabs.

1

Here is how I converted a tab delimited file to a tree format in Node

var inputFile='Tree.tab'
fs = require('fs');

tree = {} 
fs.readFile(inputFile, 'ascii', function(err, data) {
    if(err) {
        console.log(err);
    }
    lines = data.split('\r\n');
    lines.map(function(line){
        levels = line.split("\t");
        if(typeof tree[levels[0]] === "undefined") {
           tree[levels[0]] = {}
        }
        node = tree[levels[0]]
        for ( var i = 1; i < levels.length; i++) {
            if(typeof node[levels[i]] === "undefined") {
                node[levels[i]] = {}
            }
        }

    });
    console.log(tree);
});
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.