Search code examples
javascriptnode.jsfs

Write an array into an file fs


I want to create a .txt file which contains my array, using fs.

My array looks like this:

const array = [1111, 2222, 33333]

The code below allows me to write in the file and seperate each entry with a comma but I cant get them on a new line. Neither using \n nor \r\n

await fs.writeFile('Participants.txt', `${array}, \r\n`);

Thanks for your help in advance.


Solution

  • Is something like this what you are looking for?

    const array = [1111, 2222, 33333]
    var arrayLength = array.length;
    for (var i = 0; i < arrayLength; i++) {
        fs.appendFile('Participants.txt', `${array[i]}\n`);
    }