我使用push()
函数填充数组。然后,当我在customTimer
函数中尝试访问此阵列单元时,我最后作为参数传递给customTimer
函数,但我无法做到。
我尝试了一些像refObjArr[0],refObjArr["0"]
等的东西。
当我尝试console.log("qweqew",typeof refObjArr)
时,它会返回对象并尝试console.log("Array : ",arr)
我可以看到对象,但无论如何都无法访问
当我使用jQuery makeArray()function
时var arr = $.makeArray(refObjArr)
console.log("custom Timer -> refObjArr : ", refObjArr)
console.log("custom Timer -> type arr: ", typeof arr)
console.log("custom Timer -> arr: ", arr)
console.log("is Array " + jQuery.isArray(arr));
它给了我真实,但我仍然无法接受
更新:
for (var i = 0; i < detayBodyArray.length; i++) {
$.ajax({
type: "GET",
url: apiAdress + '/api/Gosterge/defParameters?ekrandetayId=' + detayId[i] + '&bodyIdIndex=' + i,
dataType: "json",
success: function (veri) {
n = detayId.indexOf(veri[2].ekrandetayId);
setRefObj[n] = {};
setRefObj[n].yS = parseInt(veri[2].value);
setRefObj[n].kS = parseInt(veri[2].value);
setRefObj[n].ekrandetayId = detayId[veri[2].index]
setRefObj[n].bodyId = bodyId[n];
setRefObj[n].gostergeTip = tip[n];
setRefObj[n].gostergeUrl = url[n];
setRefObj[n].gostergeUrlTip = urlTipi[n];
},
error: function (msg) {
alert(msg.responseText);
},
beforeSend: function (xhr, settings) { xhr.setRequestHeader('Authorization', 'Bearer ' + token); }
});
if (i === detayBodyArray.length - 1) {
customTimer();
}
}
detayBodyArray来自另一个函数。,refObjArr是一个全局数组
答案 0 :(得分:1)
我用另一种方式解决了这个问题。我创建了像这样的简单类
class refIndicator {
constructor(ekrandetayId, bodyId, gostergeTip, gostergeUrl, gostergeUrlTip, yS, kS) {
this.ekrandetayId = ekrandetayId
this.bodyId = bodyId
this.gostergeTip = gostergeTip
this.gostergeUrl = gostergeUrl
this.gostergeUrlTip = gostergeUrlTip
this.yS = yS
this.kS = kS
}
}
然后在ajax请求中填写创建和填充引用,然后推入全局数组并且它可以工作。
for (var i = 0; i < detayBodyArray.length; i++) {
$.ajax({
type: "GET",
url: apiAdress + '/api/blabla/bla/ekrandetayId=' + detayId[i],
dataType: "json",
success: function (veri) {
n = detayId.indexOf(veri[2].ekrandetayId);
setRefObj = new refIndicator(detayId[veri[2].index], bodyId[n], tip[n], url[n], urlTipi[n], parseInt(veri[2].value), parseInt(veri[2].value));
refObjArr.push(setRefObj)
if (refObjArr.length === detayBodyArray.length - 1) {
customTimer()
}
},
error: function (msg) {
alert(msg.responseText);
},
beforeSend: function (xhr, settings) { xhr.setRequestHeader('Authorization', 'Bearer ' + token); }
});
}
function customTimer() {
console.log("custom Timer -> refObjArr : ", refObjArr[2])
console.log("custom Timer -> refObjArr kS : ", refObjArr[2].kS)
console.log("custom Timer -> refObjArr edId : ", refObjArr[2].ekrandetayId)
console.log("custom Timer -> refObjArr bId : ", refObjArr[2].bodyId)
console.log("custom Timer -> refObjArr gTip : ", refObjArr[2].gostergeTip)}