2

is there any function to get the keys of an array using javascript

Also i want to reverse and array

eg:

appz_variable['412cc16e']="ABXZ";

appz_variable['axecr6fd']="YCSET";

I want the array indexes or keys in reverse order

Community
  • 1
  • 1
Anish Joseph
  • 1,026
  • 3
  • 10
  • 24
  • 1
    Arrays are usually indexed by integers. So what keys are you talking about? Are you talking about hashes? Please provide an example. – Darin Dimitrov Nov 27 '10 at 11:39
  • Dupes: http://stackoverflow.com/questions/2980242/js-objects-and-properties, http://stackoverflow.com/questions/3068534/getting-javasctipt-object-key-list, etc – sje397 Nov 27 '10 at 12:01

2 Answers2

5

I assume here you're talking about an object which some label (albeit incorrectly) an "associative array". For that situation, use a for...in loop to enumerate the object, like this:

for(var key in myObject) {
  if(myObject.hasOwnProperty(key)) {
    alert("Key: " + key + ", Value: " + myObject[key]);
  }
}

For a normal array you just loop though based on an index, like this:

for(var i=0; i<myArray.length; i++) {
  alert("Position: " + i + ", Value: " + myArray[i]);
}

The second is iterating over the array, while the first is enumerating the object...you shouldn't use a for...in loop on a normal array for example, as there are many problems that can arise.

trnelson
  • 2,715
  • 2
  • 24
  • 40
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
-1

You can index your array by numbers when it is created. perhaps like this:

appz_variable[0]['412cc16e']="ABXZ";
appz_variable[1]['axecr6fd']="YCSET";

and then you will have an "order" which you can then reverse...

Yuval A.
  • 5,849
  • 11
  • 51
  • 63