Abdul Salam Abdul Salam - 4 months ago 7
Javascript Question

Converting a variable value to (return) array in javascript?



var a = [10,20,30,40];
var b = [11,22,33,44];
var c = [40,30,20,10];

var d = ["a","b","c"];

var e = d[2];

console.log(e[0]);





How do I make javascript treat string value of variable e (i.e. string 'c') as name of array so e[0] would return 40 and not the character at position [0]. Thank you for your support and sorry if it's a dumb question.

Answer

Instead of string put the reference of the array as the element.

var d = [a, b, c];

var a = [10, 20, 30, 40];
var b = [11, 22, 33, 44];
var c = [40, 30, 20, 10];

var d = [a, b, c];

var e = d[2];

console.log(e[0]);


Another way is to use eval() method but I don't prefer that method.

Refer : Why is using the JavaScript eval function a bad idea?