I have an array of usernames that is accessible in my ejs template.
I want to create a comma-separated list of links like this:
<a href="/user1">user1</a>, <a href="/user2">user2</a> and <a href="/user3">user3</a>
Rendered html would look like this (with the usernames being links):
user1, user2 and user3 have added items to your list.
I can loop through and put them all on their own line, but how would I join that output with commas (the last one having "and")?
<% usernames.forEach(function(username){ %>
<a href="/<%= username %>"><%= username %></a>
<% }) %>
Is there anyway to join this output on comma and add 'and' with no comma for the last one?
This is what I came up with:
<% creators.forEach(function(username, i, arr){ %>
<a href="/<%= username %>"><%= username %></a><%= ( arr.length > 0 && i < arr.length-1 ? ( i == arr.length-2 ? ' and ' : ', ' ) : '' ) %>
<% }) %>
<%= creators.length > 1 ? 'have' : 'has' %> created lists or items for <%= owne
r.username %>.</p>