【问题标题】:Why Jquery Print Array elements different almost every time?为什么 Jquery Print Array 元素几乎每次都不同?
【发布时间】:2020-10-17 23:33:21
【问题描述】:

所以我编写了一个算法,其中一个循环我一直在访问 API URL 并在另一个函数中获取值。这样当我整理数据时,另一个函数几乎每次都显示不同。

例子:


function LoadManageJobs(data){
  console.log(data);
 for(index=0;index<data.results.length;index++){
  if(!data.results[index].job_area){
  data.results[index].job_area = "Unknown";
  }
  if(data.results[index].application_deadline){
                    var dateString = data.results[index].application_deadline;
                    var momentObj = moment(dateString, 'YYYY-MM-DD');
                    dateMomentString = momentObj.format('DD/MM/YYYY');
                }
  else{
    dateMomentString = "";
  }
  ******var JobId = data.results[index].job_id;
  var ApplicantsUrl = '/api/application/list/'+JobId+'/';
  get(ApplicantsUrl,LoadApplicants);******

 

   $('#manage_jobs').append('<tr class="job-items">'+
                        '<td class="title">'+
                          '<h5><a href="'+'/job-detail/'+data.results[index].slug+'">'+data.results[index].title+'</a></h5>'+
                          '<div class="info">'+
                            '<span class="office-location"><a><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-map-pin"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path><circle cx="12" cy="10" r="3"></circle></svg>'+data.results[index].job_area+'</a></span>'+
                            '<span class="job-type full-time"><a><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-clock"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>'+data.results[index].job_type+'</a></span>'+
                          '</div>'+
                        '</td>'+
                        '<td class="application"><a class="count'+index+'"></a></td>'+
                        '<td class="deadline">'+dateMomentString+'</td>'+
                        '<td class="status active">Active</td>'+
                        '<td class="action">'+
                        '<a class="preview" title="Preview"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-eye"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" cy="12" r="3"></circle></svg></a>'+
                        '<a class="edit" title="Edit"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit"><path d="M20 14.66V20a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5.34"></path><polygon points="18 2 22 6 12 16 8 16 8 12 18 2"></polygon></svg></a>'+
                        '</td>'+
                        '</tr>');
                  
 }
}

您可以在顶级星***部分看到我一直在使用 api 获取价值。

  var arr = [];
       function LoadApplicants(data){   
          arr.push(data);
          //JSON.stringify(arr);
          for(i=0;i<arr.length;i++){           
          $('.count'+i+'').html(arr[i].count+" Application(s)");
        
   }
          console.log(arr);
       }

所以我一直在这个函数中获得价值。但问题是当我打印这些值时,值的索引总是在变化。为什么会这样。请帮帮我。

【问题讨论】:

  • 需要更详细地解释问题。由于您有多个循环,因此真的不清楚具体问题是什么或哪个索引导致问题

标签: javascript html jquery arrays api


【解决方案1】:

我建议将 API 调用与结果数据的显示分开。现在,所有这些都结合在您的“get()”调用中。诊断:

  1. 调用 API,记录结果
  2. 现在将结果传递给显示方法

push() 方法将按您想要的确切顺序推送,所以比较#1 和#2。这应该可以帮助您了解其中的区别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2013-08-19
    • 2022-09-27
    • 1970-01-01
    • 2020-03-07
    • 2021-03-11
    • 2018-05-12
    相关资源
    最近更新 更多