I am retrieving JSON data containing an array and using it to create an unordered list. The issue arises when some of the entries do not have the values for "artistName" and "trackName", leading to them being displayed as Undefined. How can I modify the code below to only display entries that contain values for both "artistName" and "trackName"?
let output="<ul>";
for (let i in data.onNow.playlist) {
if (data.onNow.playlist[i].hasOwnProperty("artistName") && data.onNow.playlist[i].hasOwnProperty("trackName")) {
output+="<li>"+data.onNow.playlist[i].artistName+
" - "+data.onNow.playlist[i].trackName+"</li>";
}
}
output+="</ul>";
document.getElementById("playlist-container").innerHTML=output;