nathansebhastian / javascript-csv-array-example

JavaScript tutorial on how to convert CSV data to array
31 stars 66 forks source link

get data from outside #1

Open Willi180 opened 2 years ago

Willi180 commented 2 years ago

Hello, I thank you for Your code. I have a Question.

` myForm.addEventListener("submit", function (e) { e.preventDefault(); const input = csvFile.files[0]; const reader = new FileReader();

  reader.onload = function (e) {
    const text = e.target.result;
    const data = csvToArray(text);
    document.write(JSON.stringify(data));
  };

  reader.readAsText(input);
});

`

how can I get data from outside for use it in anover functions as Parameter?

best regards Wilhelm

nathansebhastian commented 2 years ago

Hi,

Do you mean you want to pass the array to another function as a parameter?

The csv data is saved under the variable data on line 47, so you just need to pass it into another function as you need.

In the example, I choose to write the output to the body at line 48, but you can pass it to another function there as well.

For example:

const data = csvToArray(text)
myFunction(data)
};

function myFunction(data){
  // do something with data here...
}
Willi180 commented 2 years ago

Hi, thank You!