Javascript forEach() 通过数组:如何获取上一项和下一项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38696860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:43:41  来源:igfitidea点击:

Javascript forEach() through an array: how to get the previous and next item?

javascriptarraysloopsforeach

提问by nadir

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 循环

(for i=0; i

(对于 i=0; i

Thanks a lot!

非常感谢!

回答by Nehal Gala

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.

它不起作用,因为 item 不是数组所以我们不能写 item[index-1].name。相反,我们需要使用 Fruits[index-1] 。此外,数组的第一个元素不会有前一项,最后一个元素不会有下一项。下面的代码片段应该适合你。

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);
    }
});

回答by kevin ternet

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

ForEach 循环中的回调函数接受数组作为第三个参数:

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));
});

回答by remdevtec

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

对于第一项和最后一项,您可以记录 END,也可以将其设为轮播。

option 1: mark the start and the end:

选项 1:标记开始和结束:

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

选项 2:轮播

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);
    });

回答by James

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);
  }
});