I need to set a local variable inside a for loop in a function, but part of the name of this var needs to be a string, more specifically an array index.
See the example:
function MyFunction () {
var strings = ["first","second","third","fourth"];
for (var i = 0; i < strings.length; i++) {
var "My_" + strings[i] + "_var" = "Hi, I'm the " + strings[i] + " var!";
}
}
I know this is not the correct way, I'm just illustrating what I want to do. I've already tried using window[]
or this[]
, but seems like the var becomes global.