Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 400
Abdul Salam Avatar asked Feb 17 '26 14:02

Abdul Salam


1 Answers

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?

like image 171
Pranav C Balan Avatar answered Feb 19 '26 02:02

Pranav C Balan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!