How to parse Text File to Array line by line?

I have problem with my script. I don’t know how to put text into array line by line :((

Text File

Bob
Marc
Will
Tony

Output

Array[0:"Bob", 1:"Marc", 3:"Will", 3:"Tony"]

How I can achieve this output???

I tried something like this…

const input = document.querySelector('input[type="file"]');

input.addEventListener(
  "change",
  function(e) {
    const reader = new FileReader();
    reader.onload = function() {
      const lines = reader.result.split(" ");
      let array = [];
      for (var i = 0; i < lines.length; ++i) {
        array.push(lines[i]);
      }
      console.log(array);
    };
    reader.readAsText(input.files[0]);
  },
  false
);
<input type="file">

Do you have any ideas. Thanks.

11 thoughts on “How to parse Text File to Array line by line?”

Leave a Comment