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

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

can u please tell me. how to. reverse an array using the first method
@Nick Craver - I just updated my question please go through it
@Anish - Yeah...that order isn't guaranteed, the best you can do is loop through, you can store and presents the results backwards, but it may very by browser, so I wouldn't count on the ordering (especially in IE).
I am using php to create that array . So even if I send a reversed array from php it wont be in the same order ???
@Anish - nope, not necessarily, as I said this isn't an array, it's an object.
-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...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.