2

I am new to jquery and trying something and got stuck at it, My problem is i have object with array in it i am not able to find the way to access that array from the object

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

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

and i want to access the value1 and value2 from the 0001- array from that object is there way to access that array. Any help would be great. I know with $.each i can loop through it and and then again access the array but is there any other way to do it.

3
  • There is a way but you should provide the code not the console/debugger message. Commented Jan 21, 2014 at 5:50
  • There's no jquery there, that's just a Javascript object. Commented Jan 21, 2014 at 5:50
  • Try out my new update.! Commented Jan 21, 2014 at 6:12

1 Answer 1

4

You can access it like, and keep in mind that you should use bracket notation in this context, since your keys having a starting character as a number.

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

A workaround for your new requirement,

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

console.log(xObj[xKeys[0]][0]);
console.log(xObj[xKeys[0]][1]);
Sign up to request clarification or add additional context in comments.

2 Comments

the name of array is dynamic it can be different each time so is there any way that i can use it using index like cache[0][0] i tried this but it is not working
@Curiosity It is an object so you cannot access it like an array. You should provide its key or you have to iterate into it by using $.each

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.