0

I'm trying to read a table in the form of a .csv file and put the data into a two-dimensional array. I managed to put the data into a one-dimensional array, but converting it into a two-dimensional one doesn't seem to work for me.

This is the code that I came up with. No errors are showing; however, when trying to print elements of the new two-dimensional array, they all seem empty.

const myForm = document.getElementById("myForm");
const csvFile = document.getElementById("csvFile");


myForm.addEventListener("submit", function() {

  const input = csvFile.files[0];
  const reader = new FileReader();


  reader.readAsText(input);
  reader.onload = function() {
    text = reader.result;

    var table1D = [];
    table1D = text.split(",");

    var table1DLength = table1D.length;
    var table2D = [];
    var numberOfColumns = 46;
    var numberOfRows = 32;

    for (var i = 0; i < numberOfRows; i++) {
      for (var j = 0; j < numberOfColumns; j++) {
        table2D[i] = [];
      }
    }



    for (var k = 0; k < table1DLength; k++) {
      for (var i = 0; i < numberOfRows; i++) {
        for (var j = 0; j < numberOfColumns; j++) {
          table2D[i][j] = table1D[k];
        }
      }
    }
    document.write(table2D[0][0]); // this doesn't print out anything, no matter which element

  };

  reader.onerror = function() {
    document.write("ERROR");
  };

});
<form id="myForm">
  <input type="file" id="csvFile" accept=".csv" />
  <br />
  <input type="submit" value="Submit" />

</form>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
K_Grabska
  • 1
  • 1

1 Answers1

0

There are many things not ok. Here is a fixed version using ES6 in a predictable manner

  1. I add the event handlers before I execute the thing that will trigger them
  2. I loop using nested maps
  3. I loop over number of lines and number of entries instead of hardcoding them

The code below might need more code if you have strings with commas in them

const myForm = document.getElementById("myForm");
const csvFile = document.getElementById("csvFile");
const pre = document.getElementById('pre');
window.addEventListener('DOMContentLoaded', () => {
  myForm.addEventListener("submit", e => {
    e.preventDefault();
    const input = csvFile.files[0];
    const reader = new FileReader();
    reader.addEventListener('error', function() {
      pre.textContent = "ERROR";
    });
    reader.addEventListener('load', function() {
      const text = reader.result;
      const arr = text.split(/\r?\n/)
        .map(line => line.split(","));
        console.log(arr)
      pre.textContent = arr.join('\n')
    });
    reader.readAsText(input);
  });
});
<form id="myForm">
  <input type="file" id="csvFile" accept=".csv" />
  <br />
  <input type="submit" value="Submit" />

</form>
<pre id="pre"></pre>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thank you for the response, the code seems to work well, and the array is displayed in the console. If I were to use the array in further calculations, should I do it inside the 'load' event as I did before? – K_Grabska Jan 12 '23 at 16:43
  • Yes, You can pass it to another function – mplungjan Jan 12 '23 at 18:46