0

sorry if this post is bad as I'm new to coding and stackoverflow, apologies in advance :D

var list = ["test1", "test2", "test3"];
var listName = "list";   

Can I somehow access the list via the variable to do something like -

console.log(listName[2]);

Thanks in advance!

Zaneoi
  • 1
  • Generally this isn't something we do in most modern programming languages. One way is to put your list in an object. Then you can use a string as a key in the object using bracket notation. What is the reason you want to do this? Maybe there is a better solution to your original problem that we can suggest. – Code-Apprentice Apr 19 '21 at 15:47
  • `const lists = { list1 : [...], list2 : [...] }; const listName="list2"; console.log( lists[ listName ]);` – Jeremy Thille Apr 19 '21 at 15:50

0 Answers0