Last night, I Googled a lot and couldn’t find the solution for my problem:
I have a for loop with one function in it which gets me only the latest value from the array.
So, here is the example:
obj1.route = new Routeng();
obj2.route = new Routeng();
for(var x in arrObjs) { //arrObjs = array of objects
var g = arrObjs[x];
// I can access properties of all "g" objects
Routelousse.gen(function(res) {
var pathern = res.pathern;
g.routel.staviPather(pathern);
MYOBJ.vehicles.push(g);
alert(g.name); // during the loop I always get the LAST "g" object from "arrObjs"
}, g.point);
}
It should look like this:
In this we’re passing the current
ginto that self-executing function as a different variable, rather than thegwhich is shared in the function you’re currently in (this isn’t block scope) and is changing each pass of theforloop.Also note the
forloop change…you should never use afor...inloop to iterate an Array, use a normalforloop for that.