Javascript javascript中的关联数组与对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8067590/
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
associative array versus object in javascript
提问by SAN
In my script there is a need to create a hash table, i searched in google for this. Most of the folks are recommending JavaScript object for this purpose. Problem is, some of the keys in the hash table have "." in them. I am able to create these keys easily with the associative arrays.
在我的脚本中需要创建一个哈希表,我在谷歌中搜索了这个。大多数人都为此目的推荐 JavaScript 对象。问题是,哈希表中的某些键有“。” 在他们之中。我能够使用关联数组轻松创建这些键。
I don't understand why associative arrays are bad. First thing that is mentioned in the sites that i looked at is the length property. I am coming from the Perl background, where i used hashes. Most common uses were to get the value from a key, check if a key exists, delete a key-value pair, add a key-value pair. If these are my common uses, can i safely use associative array?
我不明白为什么关联数组不好。在我查看的网站中提到的第一件事是长度属性。我来自 Perl 背景,在那里我使用了哈希。最常见的用途是从键中获取值、检查键是否存在、删除键值对、添加键值对。如果这些是我的常用用途,我可以安全地使用关联数组吗?
回答by Justin Niessner
In JavaScript, Objects are Associative Arrays...there aren't separate concepts for them. You are also able to safely use '.' in a key name, but you can only access the value using the bracket notation:
在 JavaScript 中,对象是关联数组……它们没有单独的概念。您还可以安全地使用 '.' 在键名中,但您只能使用括号表示法访问该值:
var foo = {}
foo['bar'] = 'test';
foo['baz.bin'] = 'value';
alert(foo.bar); // shows 'test'
alert(foo['baz.bin']); // shows 'value'
If you're using them already and they work, you're safe.
如果您已经在使用它们并且它们有效,那么您就是安全的。
回答by Esailija
In javascript object and array are pretty much the same thing, with array having a bit of magical functionality (autoupdating length property and such) and prototype methods suitable for arrays. It is also much easier to construct an object than using associative array:
在javascript中,对象和数组几乎是一回事,数组具有一些神奇的功能(自动更新长度属性等)和适用于数组的原型方法。构造一个对象也比使用关联数组容易得多:
var obj = {"my.key": "myValue"};
vs
对比
var obj = [];
obj["my.key"] = "myValue";
Therefore never use the array object for this but just the regular object.
因此永远不要为此使用数组对象,而只使用常规对象。
Some functionality:
一些功能:
var obj = {}; //Initialized empty object
delete a key-value pair:
删除键值对:
delete obj[key];
check if key exists:
检查密钥是否存在:
key in obj;
get key value:
获取键值:
obj[key];
add a key value pair:
添加键值对:
obj[key] = value;
回答by Dominic Goulet
Because there is no such thing as built-inassociative arrays in javascript. That's why it's bad.
因为javascript中没有内置关联数组这样的东西。这就是为什么它很糟糕。
In fact, when you use something like :
事实上,当你使用类似的东西时:
theArray["a"] = "hello, world!";
It simply creates a PROPERTY called "a" and set its value to "hello, world!". This is why the length is always 0, and why the output of alert(theArray)
is empty.
它只是创建一个名为“a”的属性并将其值设置为“hello, world!”。这就是为什么长度总是 0,以及为什么输出alert(theArray)
为空的原因。
Hope this helps!
希望这可以帮助!
回答by jAndy
Actually, an "associative array" is pretty much the same as an "array-like object" in ECMAscript. Even Arrays
are objects in ECMAscript, just with the exception to have numeric keys (which are still strings in the background), a .length
property along with some inherited methods from Array.prototype
.
实际上,“关联数组”与 ECMAscript 中的“类数组对象”几乎相同。甚至Arrays
是 ECMAscript 中的对象,只是有数字键(它们仍然是后台的字符串)、一个.length
属性以及一些从Array.prototype
.
So, a perl hash and an ECMAscript object behave similarly. You might not know that you can access object properties not only via a dot, but also with brackets and strings, like
因此,perl 哈希和 ECMAscript 对象的行为相似。您可能不知道不仅可以通过点访问对象属性,还可以使用方括号和字符串访问对象属性,例如
var myObj = { foo: 42 };
myObj.foo; // 42
myObj['foo']; // 42
knowing that, you can also use keys with .
知道这一点,您还可以使用密钥 .
var myObj = { };
myObj['hello.foo.world'] = 42;
of course, you can access that key only with the bracket notation.
当然,您只能使用括号表示法访问该键。
回答by Marc B
You can use .
in key names on JS objects (aka associative arrays) if you'd like, they're accepted without issue. The minor drawback is you can't use shortcut notations with the dotted keys, e.g.
.
如果您愿意,您可以在 JS 对象(又名关联数组)上使用键名,它们被毫无问题地接受。次要缺点是您不能使用带点键的快捷符号,例如
var x = {};
x['hello'] = 'there';
alert(x.hello);
is perfectly acceptable and will pop up an alert with 'there' in it. But if you use a dotted name:
是完全可以接受的,并且会弹出一个带有“那里”的警报。但是如果你使用带点的名字:
var x = {};
x['this.is'] = 'sparta';
alert(x.this.is);
will fail, as Javascript will look for a attribute named this
in the x object, which does not exist. There is only the this.is
attribute.
将失败,因为 Javascript 将查找this
在 x 对象中命名的属性,该属性不存在。只有this.is
属性。
回答by Dave Newton
There isn't an associative array, it's just an object.
没有关联数组,它只是一个对象。
foo.bar; // Equivalent to...
foo["bar"]; // Looks like associative array.
回答by Luo Jiong Hui
For the sake of convenience of using data, there should be no difference between an object and an array. You can think it as an object or you can think it as an associative array. At the end, you can just think of everything as data.
为了方便使用数据,对象和数组应该没有区别。您可以将其视为一个对象,也可以将其视为一个关联数组。最后,您可以将一切都视为数据。
For PHP, [ ] accepts 0, 1, or more items(array), and it is called an associative array. It is Json in PHP's coat:
$data = ["message"=>[ "id"=>405, "description"=>"Method not allowed.", "detail"=>[]], "object" => []];
For Javacript, { } accepts 0, 1, or more items(array), and it is called an object. This data format is Json:
data = {"message": { "id":405, "description":"Method not allowed.", "detail" : {}}, "object" : {}};
对于 PHP,[] 接受 0、1 或更多项(数组),它被称为关联数组。它是 PHP 外衣中的 Json:
$data = ["message"=>[ "id"=>405, "description"=>"Method not allowed.", "detail"=>[]], "object" => []];
对于 Javacript, { } 接受 0、1 或更多 items(array),它被称为object。这个数据格式是Json:
data = {"message": { "id":405, "description":"Method not allowed.", "detail" : {}}, "object" : {}};
I just call them data. The simplest way to describe data is Json, or its variants.
我只是称它们为data。描述数据的最简单方法是Json或其变体。