21

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!

3
  • 1
    fruits[index-1]... Commented Aug 1, 2016 at 10:47
  • Shouldn't you use fruits[index-1]? Commented Aug 1, 2016 at 10:47
  • 'item' holds only current object in forEach method. If you know any index, then use array[index] to get the value. Commented Aug 1, 2016 at 10:51

4 Answers 4

51

Its not working because item is not an array so we cannot write item[index-1].name. Instead, we need to use fruits[index-1] .Also, the first element of the array will not have the previous item and the last element will not have next item. Code snippet below should work for you.

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

fruits.forEach(function(item, index) {
    console.log("Current: " + item.name);
    if (index > 0) {
        console.log("Previous: " + fruits[index - 1].name);
    }
    if (index < fruits.length - 1) {
        console.log("Next: " + fruits[index + 1].name);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

14

Callback function in ForEach loop accepts the array as third parameter :

fruits.forEach((item, index, arr) => {
    console.log("Current: " + item.name);
    console.log("Previous: " + ((0 === index)? "START" : arr[index-1].name));
    console.log("Next: " + ((arr.length - 1 === index)? "END" : arr[index+1].name));
});

Comments

6

For the first and last item, you can log END, or you can make it a carousel.

option 1: mark the start and the end:

fruits.forEach(function(item,index) {
  console.log("Current: " + item.name);
  console.log("Previous: " + (0 == index)? "START" : fruits[index-1].name);  
  console.log("Next: " + (fruits.length - 1 == index)? "END" : fruits[index+1].name);
});

option 2: carousel

fruits.forEach(function(item,index) {
      console.log("Current: " + item.name);
      console.log("Previous: " + (0 == index)? fruits[fruits.length - 1].name : fruits[index-1].name);  
      console.log("Next: " + (fruits.length - 1 == index)? fruits[0].name : fruits[index+1].name);
    });

Comments

6
fruits.forEach(function(item,index) {
  console.log("Current: " + item.name);
  if (index > 0) {
    console.log("Previous: " + fruits[index-1].name);  
  }
  if (index < (fruits.length - 1)) {
    console.log("Next: " + fruits[index+1].name);
  }
});

2 Comments

would fail for 0th index and sparse array values :)
@you.know.nothing Thanks for pointing it out. Didn't think of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.