2

I have problem to save array to file, I don't now way I always getting ',' separator in new file that I assuming means new row in array. So for example I have file

this is file
with text
to check

I read this file save to array make some modification and then I want to save this file.

 fs.writeFile('to_save', file.map(function(x){return x + '\n'}));

When I do like that I always getting new file with ,

,this is file
,with text
,to check

when I try to remove firs letter using

fs.writeFile('to_save', file.map(function(x){return x = x.substring(1, x.length) + '\n'}));

It remove me t, w and t, so my question is how to get ride this separator?

1 Answer 1

4

Use join instead of map

fs.writeFile('to_save', file.join("\n"));

writeFile expects a string, a buffer, or an Uint8Array as data. It will implicitly call toString() on an array, which is why you'll get the commas.

Sign up to request clarification or add additional context in comments.

3 Comments

so why join is working, if still writeFile will call toString() ?
It will implicitly call toString() on an array, join returns a string
For the record this also worked for my appendFile needs: fs.appendFile('file_name.txt', data.join("\n"), function (err) {}...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.