Using a variable value to call an array element

后端 未结 7 1668
有刺的猬
有刺的猬 2021-01-21 08:25

I have a string variable which contains the name of an array. What I\'d like to do is access an element of that array. And write it to another variable. How can I do this?

相关标签:
7条回答
  • 2021-01-21 09:18

    If it's in the global scope, you can access your array by doing

    var sample = new Array();
    sample[0] = 'one';
    sample[1] = 'two';
    var arrayname = "sample";
    var number = (window[arrayname])[1]
    
    0 讨论(0)
提交回复
热议问题