How can I add a trailing comma after every element of an array for making a list like:
INV, INV, INV, INV
Note that the last element doesn't have a trailing comma
Currently iterating the list with array.map:
var List = React.createClass({
render: function() {
return (
<div>
{this.props.data.map(function(item) {
return <div>{item}</div>;
})}
</div>
);
}
});
var data = ["red", "green", "blue"];
React.render(<List data={data} />, document.body);
array.map((item, index) => (<div>{ (index ? ', ': '') + item}</div>)). What this will do is, check if index is valid, add a comma else blank string. And since 0 in JS is falsey, it will skip for 1st entryarr.join(',');data=data.map((x,i,arr)=>(i<arr.length-1)?x+',':x)