admin管理员组文章数量:1326079
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...
本文标签: How to get the keys of an array using javascriptStack Overflow
版权声明:本文标题:How to get the keys of an array using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742196061a2431101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论