for () issue

for () issue

Hi,

I'm currently working on a project that a script already exist but needs tweaking. Basically what it does is it performs a Get request from a service provider for job posting and display all new posts to the website.

But the issue is, if the user only has 1 post we get an error "data[i] is undefined".

  1.                 if (data.length > 0) {
                        for (var i = 0; i < 5; i++) {
                                  
                            tableList += "<ul class='sidebar-latest'><li><h4 class='post-title'><a href='" +WebSiteRoot + "/?job_id=" + data[i].AdTemplateId+ "&user_name=" + data[i].UserName + "&user_comp=" + data[i].CompanyName+ "&job_name=" + data[i].JobTitle + "'>" + data[i].JobTitle + "</a></h4><div></li><h5>" + data[i].Location + "</h5></div></ul>";
                           
                      }
this part "for (var i = 0; i < 5; i++)"  cannot be a fix number since we are not aware how many posts are there. Any idea how to remedy this?

Thanks.