admin管理员组文章数量:1221244
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>
);
}
});
}
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>
);
}
});
}
Share
Improve this question
asked Aug 7, 2017 at 12:09
userdenuserden
1,6837 gold badges27 silver badges54 bronze badges
1
- Possible duplicate of Easy way to turn Javascript array into comma-separated list? – Glubus Commented Aug 7, 2017 at 12:13
2 Answers
Reset to default 12You 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>
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>
本文标签: javascriptHow to add comma between array itemsStack Overflow
版权声明:本文标题:javascript - How to add comma between array items? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739348291a2159264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论