I am pulling data from a JSON array similar to as follows:
"abridged_directors": [{
"name": "Bill",
"id": "742790769",
"characters": ["Flint Lockwood"]
}, {
"name": "Anna",
"id": "162654477",
"characters": ["Sam Sparks"]
}, {
"name": "James",
"id": "162656402",
"characters": ["Tim Lockwood"]
}, {
"name": "Will",
"id": "770670480",
"characters": ["Chester V"]
}, {
"name": "Kristen",
"id": "770792145",
"characters": ["Barb"]
}],
I am using a loop to correctly display the result. What I am looking for is: Bill, Ann, James, Will & Kristen
Instead I am getting: Bill, Ann, James, Will, & Kristen
For the second last name I do not want a comma and the following if else statement should cover that but it doesn't seem to be working?
for (var i = 0;i < data.abridged_directors.length; i++){
if(i != 0 && i == data.abridged_directors.length-1){
// and the position of the character is greater than 0
$(document.body).append('& ' + data.abridged_directors[i].name + '<br>');
}
else if(i != data.abridged_directors.length-1 || i != data.abridged_directors.length-2){
$(document.body).append(data.abridged_directors[i].name + ', ');
}
else
$(document.body).append(data.abridged_directors[i].name + '<br>');
}
I have been looking at this for a good while now and making changes but nothing seems to work. It is possibly something small I'm not seeing so sorry if that turns out to be the case!