【问题标题】:Why my variable is not iterable when i loop为什么当我循环时我的变量不可迭代
【发布时间】:2019-04-12 02:31:00
【问题描述】:

我尝试循环二维数组,但 I 变量未定义或不可迭代,为什么? 谁能告诉我??

function sum (arr) {
  var total = 0
  for(let [a1,a2,a3] of arr){
    for(let i of [a1,a2,a3]){
      for(let j of i){
        total += j
      }
    }
    if(typeof a2 == "undefined" && typeof a3 == "undefined"){
      a2 = [0]
      a3 = [0]
    }
  }     
};


console.log(sum([
  [
    [10, 10],
    [15],
    [1, 1]
  ],
  [
    [2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
    [4],
    [9, 11]
  ],
  [
    [3, 5, 1],
    [1, 5, 3],
    [1]
  ],
  [
    [90]
  ]
]));

但是当我对另一个二维数组求和时,它可以工作,如下所示:

function sum (arr) {
  var total = 0
  for(let [a1,a2,a3] of arr){
    for(let i of [a1,a2,a3]){
      for(let j of i){
        total += j
      }
    }
  }
  return total
}  


console.log(sum([
  [
    [4, 5, 6],
    [9, 1, 2, 10],
    [9, 4, 3]
  ],
  [
    [4, 14, 31],
    [9, 10, 18, 12, 20],
    [1, 4, 90]
  ],
  [
    [2, 5, 10],
    [3, 4, 5],
    [2, 4, 5, 10]
  ]
]));

我尝试为这个二维数组循环 3 次,第一个顶部代码是每个长度在数组中是不同的 和最后的代码是一样的,

【问题讨论】:

  • 为什么要再次解构和构建数组?
  • 我也从 answrr @JonasWilms 在 stackoverflow 上看到了这种方式
  • 解构比循环更好吗?

标签: javascript arrays for-loop multidimensional-array iterator


【解决方案1】:

最好使用 concat 递归地减少数组,直到你有一个平面数组,然后将其减少到它的数字之和:

const arr = [
  [[10, 10], [15], [1, 1]],
  [[2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [4], [9, 11]],
  [[3, 5, 1], [1, 5, 3], [1]],
  [[90]],
];

const flatten = (arr) => {
  const recur = (result, item) =>
    !Array.isArray(item)
      ? result.concat(item)
      : result.concat(item.reduce(recur, []));
  return arr.reduce(recur, []);
};

console.log(
  flatten(arr).reduce((result, item) => result + item, 0),
);

【讨论】:

  • 你能用 es5 重新编码吗?我完全不明白,因为我还在学习
  • 请解释一下? @HMR
  • @ZrClassic 它使用reduce recursively 和concat 来展平多维数组。然后再次使用reduce来得到总和。以后我会建议你用谷歌搜索一些你不理解的东西。
  • @ZrClassic 如果你刚刚开始学习 Ecmascript,那么从ES6开始可能会更好
【解决方案2】:

当你说

let [a1,a2,a3] of [ [90] ])

那里没有a2或a3...

我的建议是在进入第一个 for 循环之前使用代码:

if(arr.length < 3){
   for(let y = arr.length, y > 3, y++ ){
     arr.push(0)
   }
}

干杯!

【讨论】:

    【解决方案3】:

    原因

     let [a1,a2,a3] of [ [90] ])
    

    将导致 a2 和 a3 未定义,因此在以下行中是:

     for(const i of [90, undefined, undefined])
    

    在第二个索引处确实如此:

     for(let j of undefined)
    

    这不起作用。

    【讨论】:

    • 你说Why to destructure and build up the array again?不建议使用这种方式吗? @jonasWilms
    【解决方案4】:

    您只需要移动 if 语句来检查值是否未定义,如果它在迭代这些值的代码部分之前,则将其分配为零。您收到此错误是因为那里什么都没有。

    function sumTwo(arr) {
        var total = 0
        for(let [a1,a2,a3] of arr){
        if(typeof a2 == "undefined" && typeof a3 == "undefined"){
            a2 = [0]
            a3 = [0]
          }
          for(let i of [a1,a2,a3]){
            for(let j of i){
              total += j
            }
          }
        }     
        return total
      };
    
    
      console.log(sumTwo([
        [
          [10, 10],
          [15],
          [1, 1]
        ],
        [
          [2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
          [4],
          [9, 11]
        ],
        [
          [3, 5, 1],
          [1, 5, 3],
          [1]
        ],
        [
          [90]
        ]
      ])); //prints 237
    

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 1970-01-01
      • 2015-07-11
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多