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
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
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 27, 2010 at 11:37 Anish JosephAnish Joseph 1,0263 gold badges10 silver badges24 bronze badges 2- 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 Commented Nov 27, 2010 at 11:39
- Dupes: stackoverflow./questions/2980242/js-objects-and-properties, stackoverflow./questions/3068534/…, etc – sje397 Commented Nov 27, 2010 at 12:01
2 Answers
Reset to default 5I 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.
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...