I'd like to dynamically construct keys via strings+variables.
here is an example of what I wish it could be like:
var object = {x:0,y:0};
for(i=0;i<5;i++){
if(i === 0){
object.p+i = true;
}else{
object.p+i = false;
}
}
//result
object.p0 = true;
object.p1 = false;
object.p2 = false;
object.p3 = false;
object.p4 = false;
I have an object travelling across an array of lines, it is using keys and values to keep track of which set of coordinates to start travelling to by basically switching all lines to travel towards to false except for the one it is currently travelling to, but when it reaches it, it then turns that to false and the next one to true, so it starts travelling towards it. I can do this by copying and pasting the code and changing the numbers, but above is what I'd like to accomplish, I tried doing some searching for an answer, but nothing conclusive to what I'm trying to do.
for(a=0;a<paths.length;a++){
var p = paths[a];
if(a===0 && e.p0){
if(cc(e, p, 1)){e["p" + a] = false; e["p" + a + 1] = true;}
}else if(e["p" + a]){
moveObj1ToObj2Smooth(e, p, 2);
if(cc(e, p, 1)){e["p" + a] = false; e["p" + a + 1] = true;}
}
}
this would be much easier if I could dynamically construct keys via strings+variables so that I can run a loop instead of having to copy/paste/edit 20+ different times.
any help is appreciated, thanks.