为什么 Jquery Print Array 元素几乎每次都不同?



所以我写了一个算法,其中循环我一直在点击一个 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以获得geeting值。

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);
}

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

我建议将 API 调用与结果数据的显示分开。现在,这一切都组合在您的"get(("调用中。要诊断:

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

push(( 方法将按确切的顺序推送您想要的内容,因此请比较 #1 与 #2。这应该可以帮助您了解差异。

最新更新