Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why is nobody providing the correct answer? You NEVER iterate arrays with a <code>for/in</code> loop - that is only for iterating plain objects and their keys, not for iterating the items in an array.</p> <p>You iterate arrays for a for loop like this:</p> <pre><code>var words = ['word1', 'word2', 'word3']; for (var i = 0, len = words.length; i &lt; len; i++) { // here words[i] is the array element } </code></pre> <p>Or, in a modern browser, you can use the <code>.forEach()</code> method of arrays:</p> <pre><code>var words = ['word1', 'word2', 'word3']; words.forEach(function(value, index, array) { // here value is the array element being iterated }); </code></pre> <p>See <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach" rel="nofollow noreferrer">here at MDN</a> for more info on <code>.forEach()</code>.</p> <p>ndp's reference to <a href="https://stackoverflow.com/questions/500504/javascript-for-in-with-arrays/500531#500531">this post</a> shows some good examples of things that can go wrong using <code>for/in</code> with arrays. You can make it works sometimes, but it is not the smart way to write Javascript array iteration.</p> <hr> <p>Or, in more modern times, you can use the ES6 <code>for/of</code> construct which is specially built for iterating an iterable (an array is an iterable):</p> <pre><code>var words = ['word1', 'word2', 'word3']; for (const w of words) { console.log(w); } </code></pre> <p>Or, if you want both the value and the index, you can do this:</p> <pre><code>var words = ['word1', 'word2', 'word3']; for (const [index, w] of words.entries()) { console.log(index, ": ", w); } </code></pre> <p>There are several advantages of <code>for/of</code> over <code>.forEach()</code>. To start with, you have more loop control as you can use <code>break</code>, <code>continue</code>, <code>return</code> to control the loop flow which <code>.forEach()</code> does not give you. In addition, there's no additional overhead for a callback function so, in some environments, it can be faster. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload