Reputation: 1139
I noticed incorrect output of array.join with array of strings containing (\r) character. Here's my code and output:
var list = [
"one\r",
"two\r",
"three\r",
"four\r"]
console.log(list); // ok, output: [ 'one\r', 'two\r', 'three\r', 'four\r' ]
console.log(list.join(',')); // incorrect, outputs: ,foure
It works fine in chrome/firefox consoles, just not in nodejs. I'm using node 6.11.3 on linux mint 18.3 if that matters
I can do workarounds for this, but I'm more interested in why this is happening.
Upvotes: 1
Views: 102
Reputation: 1649
Actually the string is joined successfully in your code, the output result ",foure" is caused by char '\r', which called "linefeed", line feed in Linux / macOS terminal moves the cursor to the beginning of a line, so:
the last char "e" is from "three", \r
is not "new line".
Use "\n" Carriage Return
to output new line, and just put it in "join(',\n')" instead of putting it in elements of array.
var list = [
"one",
"two",
"three",
"four"];
console.log(list); // ok, output: [ 'one', 'two', 'three', 'four' ]
console.log(list.join(',\n'));
Upvotes: 2