如何访问数组的对象?

时间:2014-01-21 05:47:49

标签: javascript arrays performance

我是jquery的新手并且尝试了一些东西而且被卡住了, 我的问题是我有数组对象,我无法找到从对象访问该数组的方法

 //My object is shown in debugging time is as below 

  cache:object 
   0001-:Array[2]
         0:value1,
         1:value2
   _prto_:object

我希望从该对象的value1数组访问value20001-是否可以访问该数组。任何帮助都会很棒。我知道$.each我可以遍历它,然后再次访问数组,但还有其他方法可以做到。

1 个答案:

答案 0 :(得分:4)

您可以像访问它一样访问它,并记住在此上下文中应使用bracket notation,因为您的密钥的起始字符为number

cache['0001-'][0] //first element on that array
cache['0001-'][1] //second element

新要求的解决方法,

var cache = {'0001-' : [0,1]};
var xKeys = Object.keys(cache);

console.log(xObj[xKeys[0]][0]);
console.log(xObj[xKeys[0]][1]);