0

This may be a question without any R&D but I am on a busy schedule and very new to these callback functions, The problem is I am getting a json payload from a webapp and I am trying to parse it , so this payload has a array of objects , but when i use this in my script i am getting only the last array index value. below is the code and attached console output for reference , please suggest where i am going wrong

var case1 = payload.Case;
var i=0;
for(i=0;i<case1.length;i++)
{
     var c1 = case1[i];
     c1.retrieveAttributes(function(){
            console.log(i+ " i");
             console.dir(c1.attributes);
     });
}

The i value in console is always 6.

enter image description here

Bharath R
  • 1,491
  • 1
  • 11
  • 23

1 Answers1

0

You're having an issue with 'closure' scope. Try passing 'i' into your function like this.

var case1 = payload.Case;
var i=0;
for(i=0;i<case1.length;i++)
{
     var c1 = case1[i];
     c1.retrieveAttributes(function(i){
            console.log(i+ " i");
             console.dir(c1.attributes);
     });
}

That should preserve the true value of 'i'.

Tyler McGinnis
  • 34,836
  • 16
  • 72
  • 77