[英]javascript multidimensional Array and associative array
i am new of javascript, i have been told by someone, he said "speak strictly, javascript doesn't have multidimensional Array and associative array ".我是 javascript 的新手,有人告诉我,他说“严格说,javascript 没有多维数组和关联数组”。 but in a book, i saw the following
但在一本书中,我看到了以下内容
var my_cars=Array();
my_cars["cool"]="Mustang";
$a=Array(Array(0,1),2);
so the opinion form he is wrong?所以他的意见是错误的? am i right?
我对吗?
JavaScript has arrays of whom their elements can be other arrays. JavaScript 有 arrays 其中的元素可以是其他 arrays。
However, JavaScript has Object
s with properties, not associative arrays.但是,JavaScript 具有
Object
的属性,而不是关联的 arrays。
[]
is much better than Array()
. []
比Array()
好得多。 Also, why not instantiate an Array
object explicitly then rely on Array
returning a new object?Array
object 然后依靠Array
返回一个新的 object?cool
on the Array
.Array
上设置属性cool
。$a
sigil?$a
印记? Why no new
operator again?new
运营商了?All the explanations of Javascript Multidimensional arrays seem to be very convoluted, after almost an hour of research I came across this simple example: Javascript 多维 arrays 的所有解释似乎都非常复杂,经过近一个小时的研究,我遇到了这个简单的例子:
var myArray = new Array();
myArray['row1'] = { 'col1':'BLARGH!!!', 'col2':'HONK!!!!' }
myArray['row2'] = { 'col1':'FOO!!!', 'col2':'BAR!!!!' }
myArray['row3'] = { 'col1':'FOUR!!!', 'col2':'GREGS!!!' }
document.write(myArray['row2']['col1'] + " - " + myArray['row3']['col2']);
//will output: FOO!!! - GREGS!!
I found it here: http://moblog.bradleyit.com/2009/06/create-multidimensional-associative.html我在这里找到它: http://moblog.bradleyit.com/2009/06/create-multidimensional-associative.html
The line: my_cars["cool"]="Mustang";
这条线:
my_cars["cool"]="Mustang";
is actually not adding a value into the array.实际上并没有在数组中添加值。 It is in fact adding a new property and value to the object
my_cars
.它实际上是为 object
my_cars
添加了一个新的属性和值。 The following code would also work same:以下代码也可以正常工作:
var my_cars = new Function();
my_cars["cool"]="Mustang";
alert(my_cars["cool"]);
var c = new Object();
c["cool"]="Corvette";
alert(c["cool"]);
To understand how this works, you can check out my blog post on arrays and maps .要了解这是如何工作的,您可以查看我在 arrays 和 maps 上的博客文章。
Btw, as @alex says, Buy A New Book.顺便说一句,正如@alex 所说,买一本新书。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.