Suppose there is a JavaScript array
myArray = {
"key1" : value1
...
"keyn" : valuen
}
My question is, can I find the integer index corresponding to, say, "key1" ?
I need both the value and the its integer position in the array!
@Slaks is correct, but maybe you can fudge it:
for(var key in myArray) {
var sudoIndex = +key.match(/\d+/g)[0],
value = myArray[key];
// do stuff
}
This assumes your keys are numbered like in your example.
for in and checked each property, while simultaneously incrementing an integer. The properties are iterated in desired order since I gave them in alphabetical order!
for initeration, but the properties wouldn't be guaranteed to come in the same order each time.