3
I have the following code:
Arr = ["meu", "objeto", "dinamico"];
Val = 100;
Eval = "";
obj = {};
for(i in Arr){
Eval += "['"+ Arr[i] + "']";
eval("obj"+Eval+"={}")
}
eval("obj"+Eval+"="+Val);
As you can see, this code dynamically generates the property obj.meu.objeto.dinamico
and adds the value of Val
in it, but this code is a bit ridiculous, I tried to make the same code without using eval()
, but I can’t imagine a decent solution.
I recommend you use the traditional loop for(i=0;...) or foreach method to iterate on vectors. for-in does not guarantee that indexes are accessed in ascending order and can also iterate over non-numeric fields (if someone says monkeypatching in Object.prototype or Array.)
– hugomg