【问题标题】:Javascript forEach() through an array: how to get the previous and next item?Javascript forEach() 通过数组:如何获取上一项和下一项?
【发布时间】:2016-12-06 09:59:54
【问题描述】:

假设我们有一个对象数组,例如:

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);
console.log("Previous: " + item[index-1].name);  
console.log("Next: " + item[index-1].name);
});

但显然它不适用于下一个和上一个项目...... 有什么想法吗?

请注意,我不想使用经典的 for 循环

(对于 i=0; 我

非常感谢!

【问题讨论】:

  • fruits[index-1]...
  • 你不应该使用fruits[index-1]吗?
  • 'item' 在 forEach 方法中仅保存当前对象。如果您知道任何索引,则使用 array[index] 获取值。

标签: javascript arrays loops foreach


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      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);
        }
      });
      

      【讨论】:

      • 对于第 0 个索引和稀疏数组值将失败 :)
      • @you.know.nothing 感谢您指出。没想到。
      【解决方案4】:

      对于第一项和最后一项,可以记录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);
      });
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 2019-04-21
        相关资源
        最近更新 更多