Javascript forEach() through an array: how to get the previous and next item?
<p>Let's say we have an array of objects like:</p>

<pre><code>var fruits = [ {name:"banana", weight:150},{name:"apple", weight:130},{name:"orange", weight:160},{name:"kiwi", weight:80} ]
</code></pre>

<p>I want to iterate through fruits and tell each time the name of the current, the previous and the next fruit.
I would do something like:</p>

<pre><code>fruits.forEach(function(item,index) {
console.log("Current: " + item.name);
console.log("Previous: " + item[index-1].name);  
console.log("Next: " + item[index-1].name);
});
</code></pre>

<p>But obviously it doesn't work for next and previous items...
Any idea?</p>

<p>Please note that I do not want to use the classic for loop </p>

<blockquote>
  <p>(for i=0; i
</blockquote>

<p>Thanks a lot!</p>