I have an array of data which i want to loop over and while looping over I want to write them to the same file. How i can achieve the same, my following code will only print the last iteration.
for (j = 0; j < arrayPart.length; j++){
fs.writeFileSync('message.txt', arrayPart[j])
}
message.txt will have the last value of arrayPart.
writeFileoverwrites the file every time you call it. See the docs, specifically "writes data to a file, replacing the file if it already exists."