Javascript: find array by name
2 answers
Perhaps, but I suggest placing these arrays as properties of the object, which makes it much easier to work with
var arrays {
arr1 : [1,2,3],
arr2 : [4,5,6]
}
var arrNum = 2;
var arr = arrays["arr" + arrNum] // arrays.arr2
Object properties can be accessed using both operator .
and names using notation ["propname"]
.
Using eval
or using the above trick is window
not recommended.
Eval'ing is usually a sign of poorly designed code, and window usage relies on Window being a global scope Variable Object - this is not part of any specification and will not necessarily work in browsers.
+5
a source to share