JavaScript - 声明关联数组的简短方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6890346/
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-23 23:38:22  来源:igfitidea点击:

JavaScript - short way of declaring an associative array

phpjavascriptarrays

提问by js-coder

Is there a short way of declaring an associative array like in PHP?

有没有像在 PHP 中那样声明关联数组的简短方法?

$myArray = array('a' => 'b'); // PHP Way

In JavaScript I'd do it this way:

在 JavaScript 中,我会这样做:

var myArray = [];
myArray['a'] = 'b';

I'm not looking for JavaScript objects.

我不是在寻找 JavaScript 对象。

回答by Karl Nicoll

Declare an object like this:

像这样声明一个对象:

var myArray = {"a": "b", "c": "d"};

... and then refer to each item like this:

...然后像这样引用每个项目:

var somethingElse = myArray["a"]; //Sets "somethingElse" to "b".

As @Chris and @Marc mention in the comments: in JavaScript, objects ARE associative arrays, and vice versa, they just refer to two different ways of doing the same thing. For Example...

正如@Chris 和@Marc 在评论中提到的:在 JavaScript 中,对象是关联数组,反之亦然,它们只是指做同一件事的两种不同方式。例如...

var somethingElse = myArray["a"];
var anotherVariable = myArray.a;

... do the same thing.

... 做同样的事。

回答by álvaro González

JavaScript does not have associative arrays. In your example, you declare myArrayas array but then you assign an object to it. So your code is not different from this:

JavaScript 没有关联数组。在您的示例中,您声明myArray为数组,但随后为其分配了一个对象。所以你的代码与此没有什么不同:

var myObject = {};
myObject['a'] = 'b';

Update:A little correction. Actually, the original code does not destroy the array. It just appends a regular property to it. That's possible because JavaScript arrays are a subset of JavaScript objects.

更新:稍微修正。实际上,原始代码并没有破坏数组。它只是向它附加了一个常规属性。这是可能的,因为 JavaScript 数组是 JavaScript 对象的子集。

回答by karim79

I'm not looking for JavaScript objects.

我不是在寻找 JavaScript 对象。

There are no "associative arrays" in JavaScript, just objects comprised of property names and values which can be treated as such. So, what you are looking for is in fact objects. In fact, this example from your question is working with an object:

JavaScript 中没有“关联数组”,只有由属性名称和值组成的对象,可以这样处理。因此,您正在寻找的实际上是对象。事实上,你问题中的这个例子正在处理一个对象:

var myArray = [];
myArray['a'] = 'b';
alert(typeof myArray); // 'object'

You initially construct the object using the array literal syntax:

您最初使用数组文字语法构造对象:

var myArray = [];

which means you have created an object which inherits from Array.prototype. So essentially, you are dealing with an object which has been augmented with the methods stored in Array.prototype.

这意味着您已经创建了一个继承自Array.prototype. 因此,本质上,您正在处理一个对象,该对象已通过Array.prototype.

To check that myArrayis actually an array (well, I would say JavaScript arrays are pseudo-arrays), you can check its constructor:

要检查它myArray实际上是一个数组(好吧,我会说 JavaScript 数组是伪数组),您可以检查它的构造函数:

alert(typeof myArray === 'object' && myArray.constructor === Array);

There's a better way which will also identify arrays constructed in different windows and frames:

还有一种更好的方法可以识别在不同窗口和框架中构造的数组:

alert(Object.prototype.toString.apply(myArray) === '[object Array]');

You can also use instanceOf:

您还可以使用instanceOf

alert(myArray instanceof Array);

or Array.isArray:

Array.isArray

alert(Array.isArray(myArray));

回答by Marc Thibault

In javascript, associative arrays, keyed collections, hashes, ... whatever you want to call them, are not a special type. All the following are good.

在 javascript 中,关联数组、键控集合、散列……无论您想怎么称呼它们,都不是特殊类型。以下都是好的。

a = {}
a[3] = 15
a.b = "c"
a['def'] = 'something'

This code produces a single valid object with the properties you would expect. All of them. You can combine a conventionally indexed array and an associative array in one object.

此代码生成具有您期望的属性的单个有效对象。他们都。您可以在一个对象中组合传统索引数组和关联数组。

As to declaring a whole bunch at once, the usual syntax is:

至于一次声明一大堆,通常的语法是:

a = {
    'key1' : 'val1',
    'key2' : val2,
    key3 : val3,
    key4 : "val4"
}