我无法弄清楚为什么对象列表在循环中的行为不同。在第二个console.log中,我得到了几个未定义的字段,并且数组中的对象多于第一个。屏幕截图和代码如下所示。有什么帮助吗?
$.ajax({
url: 'https://manage.tagman.com/tm-api-ajax/tag-instance?_fields=id,name,containers',
type: 'GET',
contentType: "application/json; charset=utf-8",
success: function(data){
var instances = data;
var test=[];
for(var k = 0; k<newEvents.length; k++){
for(var m =0; m<newEvents[k].tagInstances.length; m++){
for(var z=0; z< instances.length; z++){
if(newEvents[k].tagInstances[m].id === instances[z].id){
for(var w = 0; w < instances[z].containers.length; w++){
var getId = instances[z].containers[w].split('/');
var containerId = getId[getId.length - 1];
newEvents[k].tagInstances[m].containers = containerId;
test.push(newEvents[k].tagInstances[m].containers);
}
}
}
}
}
console.log(test);
$.ajax({
url: 'https://manage.tagman.com/tm-api-ajax/container',
type: 'GET',
contentType: "application/json; charset=utf-8",
success: function(data){
var container = data;
var test2 = [];
for(var p=0; p < newEvents.length; p++){
for(var q=0; q < newEvents[p].tagInstances.length; q++){
test2.push(newEvents[p].tagInstances[q].containers);
}
}
console.log(test2);
}
});
截图: