Tuzi
Tuzi

Reputation: 160

After "," in array set <br />

I have:

var a = ['bla bla bla', 'bla bla', 'bla']

and would like when I display this HTML, after every , to be new line (<br />), but I do not know how to do this. I tried this:

var a = ['bla bla bla', 'bla, bla', 'bla']
{a.map(b => {
  return b + <br />   //but this getting error .... whitout <br /> don't get error but doesn't have new line
})
}

I do not know how long array a will be, because of this I did not do:

{a[0] } 
<br /> 
{a[1]} 
<br /> 

Upvotes: 0

Views: 83

Answers (3)

DemiPixel
DemiPixel

Reputation: 1881

You should return a JSX Element:

a.map(b => {
  return <>{b}<br /></>
});

You need the <> and </> since you can only return a single value. More on fragments here.

Upvotes: 4

Kishor Patidar
Kishor Patidar

Reputation: 733

Instead of adding "<br />", I recommend to use built in JSON functions:

var a = ['bla bla bla', 'bla, bla', 'bla'];
a = JSON.stringify(a, null, 4)
document.getElementById("output").innerText = a
<div id="output"></div>

Upvotes: 1

Steve -Cutter- Blades
Steve -Cutter- Blades

Reputation: 5432

You don't want to use map here. Try join instead.

const fullString = a.join('<br/>')

Though, it's really unclear how you are outputting this.

Upvotes: 1

Related Questions