Javascript – Read lines from file in array

dojojavascript

I have file main.log something like :

10-01-1970 01:42:52 Bus_Power device 9 up
10-01-1970 01:42:52 External_Power device 9 up
10-01-1970 01:42:57 Bus_Power device 1 down
10-01-1970 01:42:57 Bus_Power device 2 down

Every row is one data. How to parse this in array of rows using Dojo or plain JavaScript ?

for example :

['10-01-1970 01:42:52 Bus_Power device 9 up','10-01-1970 01:42:52 External_Power device 9 up']

Best Solution

var xhr = new XMLHttpRequest();

xhr.open('GET', 'main.log', false);
xhr.send(null);

var log = xhr.responseText.split('\n');

// `log` is the array of logs you want

Note: Done synchronously, for simplicity, as no details were given about application of this functionality.