Javascript 数组声明:new Array(), new Array(3), ['a', 'b', 'c'] 创建行为不同的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8246406/
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
Javascript array declaration: new Array(), new Array(3), ['a', 'b', 'c'] create arrays that behave differently
提问by sbichenko
Consider this example Javascript code:
考虑这个示例 Javascript 代码:
a = new Array();
a['a1']='foo';
a['a2']='bar';
b = new Array(2);
b['b1']='foo';
b['b2']='bar';
c=['c1','c2','c3'];
console.log(a);
console.log(b);
console.log(c);
Results in the Firebug console are as follows:
Firebug 控制台中的结果如下:
For a (the '[]' had to be expanded by clicking on the '+' button):
对于 a(必须通过单击“+”按钮来展开“[]”):
[]
a1 "foo"
a2 "bar"
For b:
对于乙:
[undefined, undefined]
For c:
对于 c:
["c1", "c2", "c3"]
My questions are:
我的问题是:
- Am I using the array['key']='value' syntax correctly?
- Why isn't array b working as expected?
- Why are arrays a and c displayed differently in the console? It also seems that jQuery is unable to iterate through the array a with it's .each() method.
- Could you reccomend any good tutorials on Javascript array behaviour?
- 我是否正确使用了 array['key']='value' 语法?
- 为什么数组 b 没有按预期工作?
- 为什么数组 a 和 c 在控制台中显示不同?jQuery 似乎也无法使用它的 .each() 方法遍历数组 a 。
- 你能推荐一些关于 Javascript 数组行为的好教程吗?
NOTE:Google Chrome's Firebug displays only [] for array 'a', without the option to expand it.
注意:Google Chrome 的 Firebug 仅显示数组 'a' 的 [],没有扩展它的选项。
EDIT:Alright, it seems that arrays in Javascript have only numerical keys, so adding a string as a key name makes an object out of an array. But why doesn't jQuery's .each work with it?
编辑:好的,Javascript 中的数组似乎只有数字键,因此添加字符串作为键名会使对象脱离数组。但是为什么 jQuery 的 .each 不能使用它呢?
$.each(a, function ()
{
alert ('derp');
})
This code, appended to the script, produces no alerts.
此代码附加到脚本中,不会产生警报。
回答by jfriend00
Arrays have numerical indexes. So,
数组具有数字索引。所以,
a = new Array();
a['a1']='foo';
a['a2']='bar';
and
b = new Array(2);
b['b1']='foo';
b['b2']='bar';
are not adding elements to the array, but adding .a1
and .a2
properties to the a
object (arrays are objects too). As further evidence, if you did this:
不将元素添加到阵列中,但添加.a1
和.a2
特性的a
对象(数组是对象太)。作为进一步的证据,如果你这样做:
a = new Array();
a['a1']='foo';
a['a2']='bar';
console.log(a.length); // outputs zero because there are no items in the array
Your third option:
你的第三个选择:
c=['c1','c2','c3'];
is assigning the variable c
an array with three elements. Those three elements can be accessed as: c[0]
, c[1]
and c[2]
. In other words, c[0] === 'c1'
and c.length === 3
.
正在为变量分配c
一个包含三个元素的数组。这三个元素可以通过以下方式访问:c[0]
,c[1]
和c[2]
。换句话说,c[0] === 'c1'
和c.length === 3
。
Javascript does not use its array functionality for what other languages call associative arrays where you can use any type of key in the array. You can implement most of the functionality of an associative array by just using an object in javascript where each item is just a property like this.
Javascript 不会将其数组功能用于其他语言称为关联数组的内容,您可以在其中使用数组中的任何类型的键。您可以通过在 javascript 中使用一个对象来实现关联数组的大部分功能,其中每个项目只是一个像这样的属性。
a = {};
a['a1']='foo';
a['a2']='bar';
It is generally a mistake to use an array for this purpose as it just confuses people reading your code and leads to false assumptions about how the code works.
出于此目的使用数组通常是错误的,因为它只会让阅读您的代码的人感到困惑,并导致对代码如何工作的错误假设。
回答by Navin Israni
Arrays in JS have two types of properties:
JS 中的数组有两种类型的属性:
Regular elements and associative properties (which are nothing but objects)
常规元素和关联属性(只不过是对象)
When you define a = new Array()
, you are defining an empty array. Note that there are no associative objects yet
当您定义时a = new Array()
,您正在定义一个空数组。请注意,目前还没有关联对象
When you define b = new Array(2)
, you are defining an array with two undefined locations.
当您定义时b = new Array(2)
,您正在定义一个具有两个未定义位置的数组。
In both your examples of 'a' and 'b', you are adding associative properties i.e. objects to these arrays.
在“a”和“b”的两个示例中,您都向这些数组添加了关联属性,即对象。
console.log (a)
or console.log(b)
prints the array elements i.e. []
and [undefined, undefined]
respectively. But since a1/a2
and b1/b2
are associative objects inside their arrays, they can be logged only by console.log(a.a1, a.a2)
kind of syntax
console.log (a)
或分别console.log(b)
打印数组元素 ie[]
和[undefined, undefined]
。但是由于a1/a2
和b1/b2
是数组内的关联对象,因此只能通过console.log(a.a1, a.a2)
某种语法记录它们