Array.join(\n) not the way to join with a newline?
Yes, but if you print that string out it will have newlines in it:
irb(main):001:0> a = (1..4).to_a
=> [1, 2, 3, 4]
irb(main):002:0> a.join("\n")
=> "1\n2\n3\n4"
irb(main):003:0> puts a.join("\n")
1
2
3
4
So it does appear to achieve what you desire (?)
JavaScript Newline Character
Split it on /\r?\n/, in case the string includes the carriage returns with newlines.
join it with '\n', in any browser and any os.
Rails: join an array with a new line between each component
irb(main):004:0> puts ["a", "b", "c", "d"].join("\n")
a
b
c
d
You didn't show that part of the code but I suspect that the problem is that you're trying to put the result string in an HTML page. HTML ignores whitespace and that is why you don't get the newlines. You can try the solution you proposed with the <br>
and try to add .html_safe
Python - Join with newline
The console is printing the representation, not the string itself.
If you prefix with print
, you'll get what you expect.
See this question for details about the difference between a string and the string's representation. Super-simplified, the representation is what you'd type in source code to get that string.
Split elements in array and add line break, then join - Javascript
class App extends React.Component{ render(){ var arr = ["hello", "there", "world"] return( <div> {arr.map(s=><React.Fragment>{s}<br/></React.Fragment>)} </div> ) }}
ReactDOM.render(<App />, document.getElementById("app"))
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script><script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script><div id="app"></div>
How to break lines in javascript using .join
You can use a for
loop with Array#slice
.
const arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
const parts = [];
for(let i = 0; i < arr.length; i += 3){
parts.push(arr.slice(i, i + 3).join(' '));
}
console.log(parts.join('\n'));
Related Topics
Ruby Float#Round Method Behaves Incorrectly with Round(2)
How to Save Unescaped & in Nokogiri Xml
Is Every Relavant Calculation Performed Every Time the Page Is Loaded
Replacing Text in One CSV Column Using Fastercsv
How to List the Currently Available Objects in the Current Scope in Ruby
Read Input from Console in Ruby
How to Use Ruby for Shell Scripting
Rmagick Installation: Can't Find Magickwand.H
Checking If a Variable Is Not Nil and Not Zero in Ruby
Measure and Benchmark Time for Ruby Methods
Remove Substring from the String
How to Make Nokogiri Not to Convert &Nbsp; to Space
How to Use Ruby's Readlines.Grep for Utf-16 Files
Nicely Formatting Output to Console, Specifying Number of Tabs