1

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

Samir
  • 1,312
  • 1
  • 10
  • 16
Harun KARATAŞ
  • 116
  • 2
  • 11
  • 2
    `refObjArr[0]` should work. We need to see more of your code to help you - specifically, how you create the array and how you pass it to the function – Rory McCrossan Nov 24 '16 at 08:57
  • Try to reproduce your problem inside a working snippet – Weedoze Nov 24 '16 at 09:05
  • 1
    This is a combination of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) and [how Chrome displays arrays in the console](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – JJJ Nov 24 '16 at 09:07
  • @JJJ that's why i used `if (i === detayBodyArray.length - 1) { customTimer(); }` – Harun KARATAŞ Nov 24 '16 at 09:18
  • That doesn't make any difference because the Ajax calls haven't finished yet at that point. You'll have to wait until all of them are finished, see e.g. http://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-requests-are-done – JJJ Nov 24 '16 at 09:21
  • Can you make any plunker or jsfiddle, so that we can investigate the problem? – Tajkia Rahman Toma Nov 24 '16 at 09:59
  • i can't reproduce problem, lots of thing going to change on fiddle in example ajax request to my local api , detayBodyArray,veri,bodyId array,url,urltip,tip arrays and n etc. – Harun KARATAŞ Nov 24 '16 at 10:54

1 Answers1

1

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)}
Harun KARATAŞ
  • 116
  • 2
  • 11