Harun KARATAŞ Harun KARATAŞ - 11 days ago 6
Javascript Question

Can't access Array of objects

I am filling an array with objects with

push()
function. Then I'm passing as a parameter to
customTimer
function in the end when I am in the
customTimer
function i trying to access this array cells but i can't.
i tried a few things like
refObjArr[0],refObjArr["0"]
etc.
When I'm trying to
console.log("qweqew",typeof refObjArr)
it returns object and i tried
console.log("Array : ",arr)
i can see objects but i can't access anyway

Objects

when Im using 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));


it gives me true but i still can't acess

UPDATE :

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 coming from another func., refObjArr is a global array

Answer

I solve this problem with another way. I created simple class like this

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

then fill in created and fill reference in ajax request then push into global array and it works .

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