0

If I have an array:

var a = [56, 99, 2];

the only way to iterate all the contents of it that I know of is:

for (var i = 0; i < a.length; ++i) {
    // do something with a[i]
}

If I do:

for (var i in a) {
    // do something with a[i]
}

it will get the "include" and "empty" functions too, so I don't want that.

Is there any nicer way than then ++i method?

JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • by pure javascript ? no jquery or other frameworks ? – mohsen dorparasti Dec 13 '13 at 17:36
  • Ah, I guess in jQuery I could use $.each. Is there any way in pure Javascript? What about node? – JoelFan Dec 13 '13 at 17:37
  • 2
    check this post http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript – mohsen dorparasti Dec 13 '13 at 17:38
  • The `i++` method of iterating array elements is the safe way to iterate that works in all browsers. Use that way until you can abandon browsers that don't support `.forEach()` in which case you can use that. But, even then sometimes the `i++` method is simpler for some types of behavior (such as stopping the iteration early and preserving state). – jfriend00 Dec 13 '13 at 17:50

4 Answers4

1

Most modern browsers support forEach (dont know about IE ;-))

[56, 99, 2].forEach(function(element, index, array) {
    console.log(element);
});

for .. in in can actually be used (although not really recommended) if you apply following checks:

  • array.hasOwnProperty(key) == true
  • Key is an integer.
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

Yes, use .forEach https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

generally take a look at the array builtin methods, they can make your code more elegant and safe: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

danza
  • 11,511
  • 8
  • 40
  • 47
0

You need to be careful using forEach as suggested above if this is going to be run in IE8 browser. See For-each over an array in JavaScript?

Community
  • 1
  • 1
peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76
0

Try:

for (var i of a)
{
   //Do something
}

The for..of loop is, in fact, is only a part of the EcmaScript 6 proposal, and is in fact experimental, so browser support right now is limited (Firefox does support it, but you may have problems with other browsers). So you might want to go with the array.foreach() method, as others have suggested. You can read more here.

Still, IMHO, it's good to know of a technology, that will (hopefully) soon become the standard way of iterating over an array in js.

Stephan Zaria
  • 496
  • 5
  • 12
  • gives me a syntax error – JoelFan Dec 13 '13 at 17:43
  • This is a bad way to iterate arrays because it iterates all enumerable properties of the array, NOT just the array elements. – jfriend00 Dec 13 '13 at 17:48
  • My bad, should be for (**var** i... – Stephan Zaria Dec 13 '13 at 17:49
  • @StephanZaria - you can edit your question to fix the missing `var`, but this is still a bad way to iterate array elements. – jfriend00 Dec 13 '13 at 17:52
  • @jfriend00 The for **of** loop, as opposed to the standard for **in** loop, does not iterate all enumerable properties. It is problematic though, as it still only a proposition, and while implemented in firefox and IE, I'm not so sure about chrome, safari and opera support (can't check it at the moment). I will fix my answer to reflect that. – Stephan Zaria Dec 13 '13 at 18:01
  • 1
    Didn't notice you were using `of`. So you're suggesting an experimental/proposed method that isn't even implemented in all current browsers? – jfriend00 Dec 13 '13 at 18:12