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
  • 53,323
  • 11
  • 64
  • 89

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,977
  • 4
  • 17
  • 34