0

i'd like to print every iteration in my loop (1,2,3,4,5). But at this moment, it's only printing the size of my array (5).

for (var key in mapParseJson.background) {

  //sequelize
  refModelBedrawnins.find({
    where: ['POS_TILE_BE_DRAW_IN =? ',mapParseJson.background[key]            [0]],
    include: [{ model: refModelDrawableObject }]
  }).success(function(result) {

    // I'd like to print every iteration right here
    console.log(key);
  });

}
Andy
  • 61,948
  • 13
  • 68
  • 95

1 Answers1

3

You need to avoid the closure and save the var "key" value:

for (var key in mapParseJson.background) {
    //sequelize
    (function(k){
         refModelBedrawnins.find({ 
             where: ['POS_TILE_BE_DRAW_IN =? ', mapParseJson.background[k]            [0]],
             include: [{model: refModelDrawableObject}] 
         }).success(function(result) {
             console.log(k);
         });
    })(key);
}
Hacketo
  • 4,978
  • 4
  • 19
  • 34
  • I don't know why, but the function() isn't executed. (i'm using nodejs to print the result). If I console.log('test') in the anonymous function it's just printing nothing – Francis Delgado Sep 29 '14 at 13:28
  • i've edited, really sorry, I used to do it with functions .. – Hacketo Sep 29 '14 at 13:37