Javascript 使用变量值调用数组元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8836265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:44:34  来源:igfitidea点击:

Using a variable value to call an array element

javascriptarrays

提问by user1145643

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?

我有一个包含数组名称的字符串变量。我想做的是访问该数组的一个元素。并将其写入另一个变量。我怎样才能做到这一点?

var sample = new Array();
sample[0] = 'one';
sample[1] = 'two';

var arrayname = "sample";
var number = arrayname[1];  ///  Something like this, although I realize this doesn't work for obvious reasons...

I've read other questions in regards to using an variable value as a function, but I didn't understand the approach or even if this would work for the above situation.

我已经阅读了有关将变量值用作函数的其他问题,但我不理解这种方法,或者即使这适用于上述情况。

Thanks in advance!!

提前致谢!!

采纳答案by Skyrim

This should do it:

这应该这样做:

    var sample = new Array();
    sample[0] = 'one';
    sample[1] = 'two';

    var arrayname = "sample";
    var number = window[arrayname][1];
    alert(number);

回答by Pointy

There's no universal way in JavaScript to do exactlywhat you've got set up there, but you canuse string variables to access object properties in a dynamic way.

JavaScript 中没有通用的方式来完全按照您在那里设置的方式进行操作,但是您可以使用字符串变量以动态方式访问对象属性。

var objArr = { array1: [], array2: [] };

Now you can use a variable with the value "array1" or "array2" to get at those arrays:

现在您可以使用值为“array1”或“array2”的变量来获取这些数组:

var name = "array2";
objArr[name].push(14);

What you cannotdo in JavaScript is access local variables by a dynamic name, or indirectly if you prefer. You can access globalvariables that way if you have a name for the global context, which in a browser is the windowobject:

什么,你不能在JavaScript中做的是一个动态域名访问的局部变量,或间接,如果你喜欢。如果您有全局上下文的名称(在浏览器中是对象),则可以通过这种方式访问全局变量window

window[ name ].push(17);

However there's no way to get a similar name for a local scope.

但是,无法为本地范围获得类似的名称。

edit— @Neal points out (and is downvoted mercilessly) that eval()can do what you want, but a lot of people recommend staying far away from eval()unless it's absolutely unavoidable (which is really rare). I've trained myself to ignore it so well that I always forget about it when questions like this are asked (which is, oddly, quite often on SO, though in my programming practice I never find myself wanting to do this).

编辑-@Neal 指出(并且被无情地拒绝)eval()可以做你想做的事,但很多人建议远离,eval()除非它绝对不可避免(这真的很少见)。我已经训练自己很好地忽略它,以至于在被问到这样的问题时我总是忘记它(奇怪的是,在 SO 上经常出现这种情况,尽管在我的编程实践中,我从来没有发现自己想要这样做)。

回答by jabclab

You could use something like:

你可以使用类似的东西:

var arrayName = "sample",
    number = window[arrayname][1];

回答by Naftali aka Neal

I mean you can use eval, but it is not the best approach:

我的意思是你可以使用 eval,但这不是最好的方法:

eval("var arrayname = sample");
var number = arrayname[1];  

Why not just do:

为什么不这样做:

var arrayname = sample;
var number = arrayname[1];  

回答by Grooveek

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]

回答by itea

var sample = new Array();
sample[0] = 'one';
sample[1] = 'two';
var obj = {"sample": sample}

var arrayname = "sample";
var number = obj[arrayname][1];

回答by Sergey Kudriavtsev

Out of my head...

意料之外...

First declare some global variable

首先声明一些全局变量

var buf;

Then

然后

var sample = new Array();
sample[0] = 'one';
sample[1] = 'two';

var arrayname = "sample";
eval('buf=' + arrayname + ';');
var number = buf[1];

Damn undesirable way but does what you want...

该死的不受欢迎的方式,但做你想做的......