Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate string lines in NodeJS

I get a buffer (and I can make it a string) from child_process.exec() in NodeJS. I need to iterate over the lines of the output string. How would I do this?

like image 761
Tower Avatar asked Dec 16 '22 09:12

Tower


1 Answers

One way to avoid splitting the whole thing in memory is to process it one line at a time

var i = 0;
while (i < output.length)
{
    var j = output.indexOf("\\n", i);
    if (j == -1) j = output.length;
    .... process output.substr(i, j-i) ....
    i = j+1;
}
like image 98
6502 Avatar answered Dec 21 '22 22:12

6502