C
C
campus12018-07-16 16:28:56
React
campus1, 2018-07-16 16:28:56

How to properly render data?

I have this structure:

[
  {
    name: "test",
    tests: ["one", "two"]
  }
];

I need to render tests content. Only if there are more than 1, after each there is a coma and a space
. As I thought to solve:
renderTest = test => {
    return test.map((el, index) => {
      return test.length > 1 ? (
        <span key={index}>{el}, {" "} </span>
      ) : (
        <span key={index}>{el}</span>
      );
    });
  };

But something seems to me a shit code, because it can come out one , two ,
Tell me how it will be right ??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Wolf, 2018-07-16
@campus1

<span>{test.tests.join(', ')}</span>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question