Good evening! I really need help. There is a txt file http://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/bradforddata.txt you need to somehow convert it into an array with objects, for further display through AngularJS, help plz

  • For this you need some kind of server language, preferably. - Stepan Kasyanenko

1 answer 1

You can get by ajax:

fetch('http://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/bradforddata.txt').then(response => response.text).then(text=> { console.log(text); }); 

If you deal with CORS, of course. I would like something like this:

 const text = `Bradford Location 414900E 435200N, Lat 53.813 Lon -1.772, 134 metres amsl Estimated data is marked with a * after the value. Missing data (more than 2 days missing in month) is marked by ---. Sunshine data taken from an automatic Kipp & Zonen sensor marked with a #, otherwise sunshine data taken from a Campbell Stokes recorder. yyyy mm tmax tmin af rain sun degC degC days mm hours 1908 1 5.0 -1.4 21 --- 29.7 1908 2 7.3 1.9 8 --- 71.9 1908 3 6.2 0.3 13 --- 101.4 1909 3 5.6 -0.3 17 --- 58.7 1909 4 12.2 3.3 3 --- 188.9 1909 5 14.7 4.8 2 --- 216.8 1909 6 15.0 7.5 0 --- 139.5 1909 7 17.3 10.8 0 --- 151.2 1910 9 15.0 8.5 0 --- 106.1 1910 10 12.6 7.3 0 --- 69.7 1910 11 5.5 -0.4 17 --- 66.0 1910 12 8.0 3.6 3 --- 24.0 1911 1 5.8 1.8 6 33.0 23.5 1911 2 6.9 1.3 8 91.7 65.8 1911 3 6.4 1.9 2 25.1 83.6 1911 4 10.0 3.9 3 26.4 128.3 1911 5 16.3 7.9 0 24.6 192.3 1911 6 17.8 9.0 0 84.3 225.3 2017 7 19.9 11.8 0 74.2 172.0* Provisional 2017 8 19.0 11.3 0 59.8 175.3* Provisional `; let fields; console.log(text.split('\n').reduce((result,string) => { const words = string.split(/\s+/g); if(words.length === 8){ if(fields){ const obj = {}; fields.forEach((field, index) => obj[field] = words[index]); result.push(obj); }else{ fields = [...words]; } } return result; }, []));