Write the elements of an array in JSX React block with a comma spacer between and a dot in the end
We have an array of strings in React Application. Let's call it 'selectedParticipants'. And for the summary page we need to write all the members of array in one line with comma ',' between elements and dot '.' in the end in JSX block. Here is a small snippet.
The code below writes all the members of array in one line with comma ',' between elements and dot '.' after the last one. This text 'as it is' can be used in JSX block.
const selectedParticipants:string[] = [ 'user1@markimarta.com', 'user2@markimarta.com', 'user3@markimarta.com', 'user4@markimarta.com' ]
Participants: <div>{
selectedParticipants.map ( (u, index) => {
const spacer: string = ( index === selectedParticipants.length - 1) ? '.' : ','
return (
<span key={`uk${u}`}>{u}{spacer}</span>
)
})
}</div>
The result is:
user1@markimarta.com, user2@markimarta.com, user3@markimarta.com, user4@markimarta.com.