Posts

Showing posts with the label fetch-api

Get an array of values using fetch api javascript

Image
Clash Royale CLAN TAG #URR8PPP Get an array of values using fetch api javascript 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. How do you know that nothing is added? – PM 77-1 10 mins ago The console.log(arr) prints an empty array – nazschi 10 mins ago ...