I am using fetch api to read a txt file via javascript. I want to load the contents of the txt file which are separated by new line in an array.
Text file:
A
B
C
I need it in the following format:
arr = ["A", "B", "C"]
Below is the code I tried
var arr = []
fetch('file.txt')
.then(function(response) {
return response.text();
}).then(function(text) {
arr.push(text)
console.log(text)
});
console.log(arr)
Nothing gets added to my array, however the data from the text file gets printed on the console.