title: Iterate an array as a pair (current, next) in JavaScript
body: <p>In the question <a href="https://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python">Iterate a list as pair (current, next) in Python</a>, the OP is interested in iterating a Python list as a series of <code>current, next</code> pairs.  I have the same problem, but I'd like to do it in JavaScript in the cleanest way possible, perhaps using <a href="https://lodash.com/" rel="noreferrer">lodash</a>.</p>
  
  <p>It is easy to do this with a simple <code>for</code> loop, but it doesn't feel very elegant. </p>
  
  <pre><code>for (var i = 0; i &lt; arr.length - 1; i++) {
    var currentElement = arr[i];
    var nextElement = arr[i + 1];
  }
  </code></pre>
  
  <p>Lodash almost can do this:</p>
  
  <pre><code>_.forEach(_.zip(arr, _.rest(arr)), function(tuple) {
    var currentElement = tuple[0];
    var nextElement = tuple[1];
  })
  </code></pre>
  
  <p>The subtle problem with this that on the last iteration, <code>nextElement</code> will be <code>undefined</code>.  </p>
  
  <p>Of course the ideal solution would simply be a <code>pairwise</code> lodash function that only looped as far as necessary. </p>
  
  <pre><code>_.pairwise(arr, function(current, next) {
    // do stuff 
  });
  </code></pre>
  
  <p>Are there any existing libraries that do this already?  Or is there another nice way to do pairwise iteration in JavaScript that I haven't tried?</p>
  
  <hr>
  
  <p>Clarification:  If <code>arr = [1, 2, 3, 4]</code>, then my <code>pairwise</code> function would iterate as follows: <code>[1, 2]</code>, <code>[2, 3]</code>, <code>[3, 4]</code>, not <code>[1, 2]</code>, <code>[3, 4]</code>.  This is what the OP was asking about in <a href="https://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python">the original question for Python</a>.</p>