I have json like this:
obj = [{"name":"Jim Smith","role":"Author"},{"name":"Julie Smith","role":"Author"}]
I would like to print the name of the authors like this:
<div class="authors">Authored by Jim Smith & Julie Smith</div>
I am iterating through the object like this:
$.each( obj, function(k,v) {
$('.authors').append('<span>' + v.name + '</span>');
});
This outputs
<div class="authors">Authored by Jim SmithJulie Smith</div>
Ideally, a solution will also take into account 1, 2, and 2+ authors such that if there are more than 2 authors, only the last author will have the & prefix the value.
For example: if there are three authors: Jim Smith, Julie Smith, & John Smith
Notice the usage of the oxford comma.
Should I wrap it in an if statement? How best to iterate through an $.each loop while keeping track of the length of the return and which iteration is active?