Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for ... in loop with string array outputs indices

Tags:

When I write some javascript such as this:

var words = ['word1', 'word2', 'word3']  for (word in words) {     console.log(word) } 

The resulting output is the numeric indices of the respective word. I did some searching on Google and I couldn't find the exact reason for this behavior. I'm guessing that this is completely expected behavior but I would like to know the reason why.

Thanks!

like image 496
PolandSpring Avatar asked Sep 20 '11 04:09

PolandSpring


People also ask

Can you use for in loops for arrays?

For Loop to Traverse Arrays. We can use iteration with a for loop to visit each element of an array. This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.

Can you declare an array in a for loop?

You can declare (define) an array inside a loop, but you won't be able to use it anywhere outside the loop. You could also declare the array outside the loop and create it (eg. by calling new ...) inside the loop, in which case you would be able to use it anywhere as far as the scope the declaration is in goes.

Can an array index be a string?

Actually, it has very much to do with the question (specifically, yes, you CAN use a string as an index, but not in the obvious way that the original querier wants).

How do you add indices to an array?

You want to explicitly add it at a particular place of the array. That place is called the index. Array indexes start from 0 , so if you want to add the item first, you'll use index 0 , in the second place the index is 1 , and so on. To perform this operation you will use the splice() method of an array.


1 Answers

Why is nobody providing the correct answer? You NEVER iterate arrays with a for/in loop - that is only for iterating plain objects and their keys, not for iterating the items in an array.

You iterate arrays for a for loop like this:

var words = ['word1', 'word2', 'word3'];  for (var i = 0, len = words.length; i < len; i++) {     // here words[i] is the array element } 

Or you can use the .forEach() method of arrays:

var words = ['word1', 'word2', 'word3'];  words.forEach(function(value, index, array) {     // here value is the array element being iterated }); 

See here at MDN for more info on .forEach().

ndp's reference to this post shows some good examples of things that can go wrong using for/in with arrays. You can make it works sometimes, but it is not the smart way to write Javascript array iteration.


Or, in more modern times, you can use the ES6 for/of construct which is specially built for iterating an iterable (an array is an iterable):

var words = ['word1', 'word2', 'word3'];  for (const w of words) {     console.log(w); } 

Or, if you want both the value and the index, you can do this:

var words = ['word1', 'word2', 'word3'];  for (const [index, w] of words.entries()) {     console.log(index, ": ", w); } 

There are several advantages of for/of over .forEach(). To start with, you have more loop control as you can use break, continue, return to control the loop flow which .forEach() does not give you. In addition, there's no additional overhead for a callback function so, in some environments, it can be faster.

like image 59
jfriend00 Avatar answered Dec 29 '22 23:12

jfriend00