How to render react components by using map and join?

后端 未结 14 2168
南旧
南旧 2020-12-07 15:30

I have one component which is going to display Array of String. The code looks like this:

React.createClass({
  render() {
     
this.props
14条回答
  •  失恋的感觉
    2020-12-07 16:08

    Use nested array to keep "," outside.

      
    {this.props.data.map((element, index) => index == this.props.data.length - 1 ? {element} : [{element}, ", "])}

    Optimize it by saving the data to array and modify last element instead of checking if its last element all the time.

      let processedData = this.props.data.map((element, index) => [{element}, ", "])
      processedData [this.props.data.length - 1].pop()
      
    {processedData}

提交回复
热议问题