Proper way to render integer array as a string with commas in JSX

Viewed 51

Is there a way to render something like this: ["1", "2","3"] as1,2,3,4,5 in React?

render(){
  <p>{this.numberResult()} </p> 
}

this.numerResult returns ["1", "2","3"] which ends up looking like: 123 in the browser but I would like it to return 1,2,3

3 Answers

Use join().

Sample Example:

const joined = ["1", "2","3"].join(",")
console.log(joined);

render(){
  <p>{this.numberResult().join(",")} </p> 
}

If you want to show numbers comma separeted, use join:

render(){
  <p>{(this.numberResult() || []).join(', ')} </p> 
}
render(){
  <p>{this.numberResult().join(", ")</p> 
}
Related