Let's say we have an array of objects like:
var fruits = [ {name:"banana", weight:150},{name:"apple", weight:130},{name:"orange", weight:160},{name:"kiwi", weight:80} ]
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:
fruits.forEach(function(item,index) {
console.log("Current: " + item.name);
console.log("Previous: " + item[index-1].name);
console.log("Next: " + item[index-1].name);
});
But obviously it doesn't work for next and previous items... Any idea?
Please note that I do not want to use the classic for loop
(for i=0; i
Thanks a lot!
fruits[index-1]...fruits[index-1]?