5

How can I add a comma between the values, when using map function to print out all the values? (Using React, that's why I have the key etc.)

{ cars.map(car => {
    return(
      <p key={car.id}>{car.title} ,</p>
    );
}); }

This is how I would like the result to be, with no comma in the end of the last array item:

Audi, Nissan, Mazda, Toyota

Should I do it somehow like this?

{ cars.map((car, index) => {
  const separator = ", ";
  if(index === car.length - 1) {
     return(
       <p key={car.id}>{car.title} + separator </p>
     );
  }
}); 
}
userden
  • 1,615
  • 6
  • 26
  • 50
  • Possible duplicate of [Easy way to turn Javascript array into comma-separated list?](https://stackoverflow.com/questions/201724/easy-way-to-turn-javascript-array-into-comma-separated-list) – Glubus Aug 07 '17 at 12:13

2 Answers2

12

You can know which call to map's callback is the last by using the index argument you get passed:

{cars.map((car, index) => {
    return(
        <p key={car.id}>{car.title} {index < cars.length - 1 ? ", " : ""}</p>
    );
})}

But note that p is usually a block element, so the cars would be stacked rather than shown inline with spaces (and commas) between them. I'd use span instead (although you can mark the p as inline if you want). I've used span below.

You can also use a concise arrow function rather than a verbose one with a function body:

{cars.map((car, index) =>
    <span key={car.id}>{car.title} {index < cars.length - 1 ? ", " : ""}</span>
)}

Live example:

const cars = [
    {id: 1, title: "Ford"},
    {id: 2, title: "Toyota"},
    {id: 3, title: "Lexus"},
];

ReactDOM.render(
    <div>{cars.map((car, index) =>
        <span key={car.id}>{car.title}{index < cars.length - 1 ? ", " : ""}</span>)
    }</div>,
    document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

In a comment somewhere you asked how to make the space non-breaking (though I think that may have been because you were using inline-block with a p element, and so normal spaces weren't being rendered). To do that, just replace ", " with ",\u00A0" above.

const cars = [
    {id: 1, title: "Ford"},
    {id: 2, title: "Toyota"},
    {id: 3, title: "Lexus"},
];

ReactDOM.render(
    <div>{cars.map((car, index) =>
        <span key={car.id}>{car.title}{index < cars.length - 1 ? ",\u00A0" : ""}</span>)
    }</div>,
    document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Why would you want a no-break space for `, `? – Haroen Viaene Jul 16 '21 at 09:14
  • 1
    @HaroenViaene - That's a really good question. :-) I think there's a deleted comment somewhere where the OP asked for it to be non-breaking, I **think** because they were using `p` and I inexplicably used `inline-block` on the `p` to make things appear in a line...and normal spaces between `inline-block` elements aren't rendered. (I should have used `inline`). The comment is gone, but I must have had *some* reason for doing it (and for making [this comment](https://stackoverflow.com/questions/45546576/45546627#comment78054502_45546859)). But it was really weird, I've fixed it, thanks! – T.J. Crowder Jul 16 '21 at 09:45
6

Instead of complicating your markup and adding TextNodes between the Paragraphs, how about using CSS. It doesn't always have to be code.

/* just making the paragraphs inline */
div > p { display: inline-block; }

/* appending the commas */
div > p:after { content: ", " }

/* removing it for the last index */
div > p:last-of-type:after { content: "" }
<div>
  <p>Audi</p>
  <p>Nissan</p>
  <p>Mazda</p>
  <p>Toyota</p>
</div>

<div>
  <p>Audi</p>
  <p>Nissan</p>
  <p>Mazda</p>
  <p>Toyota</p>
</div>
Thomas
  • 11,958
  • 1
  • 14
  • 23
  • 1
    Ha! This (with or without the `inline-block`) is what the OP should do if pseudo-elements are an option (e.g., it doesn't matter if the commas are actually in the content -- for copy & paste, etc). – T.J. Crowder Aug 07 '17 at 12:30
  • 1
    The `inline-block` is not important. I just wanted to get the result as showed in the question, `Audi, Nissan, Mazda, Toyota` in a single row, and do it with with paragraphs in the markup. That's all the `display: inline-block` is there for, it has nothing to do with the `:after { content: ", " }`. – Thomas Aug 07 '17 at 12:39
  • 1
    @userden: To get the non-breaking spaces you were looking for with the above, one option would be `content: ",\00A0"`. – T.J. Crowder Aug 07 '17 at 12:59