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>
);
}
});
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…