嗨,我如何使用Javascript引用锯齿状阵列中的元素 [英] Hi, How Do I Refer To An Element In Jagged Array Using Javascript
问题描述
var arr = {1:[23,34,45],2:[14,22,42],3:[55,66,77]};
我如何推荐使用javascript在上面声明的锯齿状数组中42?
var arr={1:[23,34,45], 2:[14, 22, 42], 3:[55, 66,77]};
how do i refer to "42" in the above declared jagged array using javascript??
推荐答案
这不是锯齿状数组。它的一个对象 [ ^ ],其中1,2和3为键,数组为值。 />
你可以用这种方式访问42
:
That's not a jagged array. It's an Object[^] with 1, 2 and 3 as keys and arrays as values.
You can use this way to access42
:
arr[2][2]
arr [2]
指的是数组 [14,22,42]
(你给了钥匙 2
),最后一个 [2]
获取数组中的第三个元素,即42。
arr[2]
refers to the array [14, 22, 42]
(which you gave the key 2
), and the last [2]
takes the third element from the array, which is 42.
你有解决方案1,但我认为你缺乏对JavaScript对象如何工作的理解。重要的是要理解它们都是关联数组。请参阅:
http://en.wikipedia.org/wiki/Associative_array [< a href =http://en.wikipedia.org/wiki/Associative_arraytarget =_ blanktitle =New Window> ^ ],
http://en.wikipedia.org/wiki/JavaScript#Dynamic [ ^ ]。
有什么区别? />
You got Solution 1, but I think you lack the understanding how JavaScript objects work. It's important to understand that they all are associative arrays. Please see:
http://en.wikipedia.org/wiki/Associative_array[^],
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^].
What is the difference between
var someArray = [11, 12, 13];
和
and
var someObject = {0:11; 1:12: 2:13 }; //?
只有一个:someArray拥有属性长度
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array [ ^ ],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects [ ^ ]。
其他一切都是一样的;您通过索引访问该属性:
Only one: someArray have the property length
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects[^].
Everything else is the same; you access the property through index:
var first = someObject[0]; // 11
另请参阅:
https:/ /developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object [ ^ ],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects [ ^ ],
< a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object -Oriented_JavaScrip t [ ^ ]。
See also:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript[^].
这篇关于嗨,我如何使用Javascript引用锯齿状阵列中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!