I've a question about the foreach in javascript
I've the following array in javascript:
puntos":{"2":"punto dos","3":"punto tres","4":"punto cuatro","5":"punto cinco","1":"punto uno"}
If I wanna do a foreach I use the following:
for (var k in puntos){
if (puntos.hasOwnProperty(k))
{
console.log("Key is " + k + ", value is" + puntos[k]);
}
}
The secuence:
puntos":{"2":"punto dos","3":"punto tres","4":"punto cuatro","5":"punto cinco","1":"punto uno"}
The output is :
Key is 1, value ispunto uno
Key is 2, value ispunto dos
Key is 3, value ispunto tres
Key is 4, value ispunto cuatro
Key is 5, value ispunto cinco
My Question is why when I iterate over the array (puntos) the foreach alter the secuence of my array?
The secuence:
puntos":{"2":"punto dos","3":"punto tres","4":"punto cuatro","5":"punto cinco","1":"punto uno"}
The expected output
Key is 2, value ispunto dos
Key is 3, value ispunto tres
Key is 4, value ispunto cuatro
Key is 5, value ispunto cinco
Key is 1, value ispunto uno