Estoy escribiendo un programa que toma una cantidad de archivos csv (para este ejemplo, elegí 2), hace algunos cálculos y luego devuelve un archivo csv al final.
Actualmente puedo leer un solo archivo gracias a addEventListener , pero no sé cómo obtener la información del archivo fuera de la función que paso a addEvenListener (la función se llama prueba en el código).
Soy un poco principiante en todo esto, así que no estoy seguro de que esta sea la solución óptima. Estoy abierto a otras soluciones/enfoques en Javascript vainilla, ya que solo se ejecutará localmente.
Aquí está el código:
function getData() { const tritonReader = new FileReader(); //the first file reader const [triton] = document.querySelector('#Triton').files; const freewheelReader = new FileReader(); //the second file reader const [freewheel] = document.querySelector('#Freewheel').files; var tritonRevenues = [[0,0],[0,0],[0,0]]; function test() { var tritonString = tritonReader.result; const rows = tritonString.split('\n').slice(1); //creating an array with the rows, splitting by line break tritonRevenues = get2DArray(rows, ","); //creates the 2d array from the previous array (rows) var bla = createTritonRev(tritonRevenues);//does the calculations I need and returns the final array //result_csv = arrayToCsv(finalArray); //turns the array into a string formatted as a csv //downloadBlob(result_csv, 'Revenues.csv', 'text/csv;charset=utf-8;') //creates the csv file from the string } tritonReader.addEventListener("load", test, false); //adds the event listener to read the file if (triton) { tritonReader.readAsText(triton); } if (freewheel) { freewheelReader.readAsText(freewheel); } }```