React Render Array in Mapped Return

后端 未结 2 1391
时光取名叫无心
时光取名叫无心 2021-01-13 20:35

I have a react component that is rendering some results. The problem I am having is with result.participants. This is an array, so map is rendering both names without any sp

相关标签:
2条回答
  • 2021-01-13 20:41

    And if you don't care about wrapping each participant in a <span>, you could simply use the built-in Array.prototype.join method:

    <li key={result.participants}>Participants: {result.participants.join(', ')} </li>,

    0 讨论(0)
  • 2021-01-13 20:56

    Like a commenter mentioned, you should not put bare <a> and <p> tags into a <ul> directly. But that is beside the point, this question is about array handling in React.

    You should be able to use a control flow structure nested in JSX like this:

    {
      result.participants.map(function(participant, idx) {
        if (idx == result.participants.length - 1) {
          return (
            <span>{participant}, </span>
          );
        } else {
          return participant;
        }
      })
    }
    
    0 讨论(0)
提交回复
热议问题