I want to add a comma between a set of strings using the .each() function except for at the end. How is it possible to get rid of that last comma?
Asked
Active
Viewed 231 times
0
-
7Have you tried using `join`? – elclanrs Jun 16 '14 at 18:34
-
1http://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string – j08691 Jun 16 '14 at 18:36
-
2When you say "set of strings", what do you actually have? a "set of strings" could be an array, a string, an object, who knows. – Kevin B Jun 16 '14 at 18:40
-
@j08691 That worked perfectly, thank you! – user2512696 Jun 16 '14 at 18:44
2 Answers
1
-
Of course, after the each you'll have to append the last item for this to be a complete solution. – Kevin B Jun 16 '14 at 18:38
-
3
-
Cause I don't realize I was using an Id. Despite that, replace with your selector, and the syntaxt :not(:last) would be the right one. – Nico Jun 16 '14 at 18:44
0
FWIW, modern browsers allow you to do this with CSS, very similar to the jQuery solution:
.item:not(:last-of-type)::after {
content: ', '
}
Tomalak
- 322,446
- 66
- 504
- 612