【发布时间】: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