As the defination says for-in loop is used to loop through the properties of an object ,than why is it looping the element of an array?
var arr = ['a','b','c'], indexes = [];
Array.prototype.each = function() {/*blah*/};
for (var index in arr) {
indexes.push(index);
}
indexes; //["0", "1", "2", "each"]
why are 0,1,2 enumerated?They are not the properties
5 Answers 5
Quote from the documentation:
for..in should not be used to iterate over an Array where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for...in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.
Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.forEach or the non-standard for...of loop) when iterating over arrays where the order of access is important.
The key here holding the answer to your question is the following sentence:
Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties.
And the following sentence sums it up:
for..in should not be used to iterate over an Array where index order is important.
2 Comments
arr.length returns only the number of elements in the array, properties are ignored. But an array could also contain properties. Consider the following code for example: var arr = [1, 2, 3]; arr.foo = 'bar';. Now arr.length will return 3 but try a for...in loop and you will see the mess it creates. You should not use for...in with arrays. Javascript is a strange beast indeed.Each index in the array for which the array has an element is a property of that array. So this is basically what your array looks like behind the scenes:
>>> arr
{
0: 'a',
1: 'b',
2: 'c',
'each': function() {}
'length': 3
};
These keys are enumerable which is the reason why you're seeing them in your output.
2 Comments
The for in loop iterates over keys, not values. So it's giving your the array indexes 0, 1, 2 not the values.
You could do it like this but it's bad practice to use a for in on an array.
for (var index in arr) {
indexes.push(arr[index]);
}
You should use a regular for loop
for (var i = 0; i < arr.length; i++) {
indexes.push(arr[i]);
}
Comments
for...in interates over the enumerable properties of an Object. The enumerable property of Array is the index.
More information can be found here.
AMCAScript 6 defines the for...of operator which allows iteration over the values. However this has not yet been adopted.
1 Comment
Yes, if you really want to use for in and not use the keys you can make the values into keys like this e.g.:
var arr = {'a':1,'b':1,'c':1};
for(var index in arr)
indexes.push(index);
same as setting arr['a']=1 etc. It's true that for..in iterates over the keys - not the values.
var index in arrmeans thatindexwill take on the index on every value inarr. To get the value you need to dereferencearrwithindex. i.e:arr[index]"1" in arr; // true