Wednesday 29 March 2017

Ideal way to append line-separated list to file?

Let's say I have to append a line-separated list to a file at different points in time, like, say, in different event loopsvar array1 = ["foo","bar","queff"]; var array2 = ["lorem","ipsum","diksum"]; fs.appendFileSync('test.txt', array1.join("\r\n")); //Some time later fs.appendFileSync('test.txt', array2.join("\r\n")); The problem with that is that the last element of the first list and the first element of the second list are put right after one anotherNow if I did thisfs.appendFileSync('test.txt', array1.join("\r\n") + "\r\n"); Then the file will have an extra line break at the end.That was just a simple example, but if, say, these were in different loops, then I might not be able to just do a single fs.appendFileSync with them both together

Submitted March 29, 2017 at 06:55PM by DoomTay

No comments:

Post a Comment