简体   繁体   English

TypeError:无法读取未定义的属性“ length”(由功能参数定义)

[英]TypeError: cannot read property 'length' of undefined (defined by function parameter)

I'm trying to make the simple code below works, but always got the following error: TypeError: cannot read property 'length' of undefined . 我试图使下面的简单代码有效,但始终出现以下错误: TypeError:无法读取undefined的属性“ length”

function multiplyAll(arr) {


     var product = 1;

      if (arr === undefined) {
        return "Undefined Array!";
      } else {


    for (var i = 0; i < arr.length; i++) {
      for (var j = 0; j < arr[i].length; i++) {
        product *= arr[i][j];
      }
    }
    return product;


     }
    }


    multiplyAll([[7,2],[6,4],[5,8,9]]);

What is the problem? 问题是什么?

This appears to be a copy&paste/typo error: 这似乎是复制和粘贴/错字错误:

for (var i = 0; i < arr.length; i++) {
      for (var j = 0; j < arr[i].length; i++)

In the inner for loop, you're incrementing i instead of j . 在内部for循环中,您将递增i而不是j You're going off the end of the array, making arr[i] undefined. 您将离开数组的末尾,使arr[i]未定义。

You could have solved this by monitoring the values of i and j using a debugger or calls to console.log . 您可以通过使用调试器或调用console.log监视ij的值来解决此问题。 You would have noticed that i was incrementing too fast, while j remained at 0. 您可能已经注意到, i增长速度太快了,而j保持为0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 leaflet Uncaught TypeError: 无法读取未定义和 markerClusters 的属性“长度”未定义 - leaflet Uncaught TypeError: Cannot read property 'length' of undefined and markerClusters is not defined Javascript:简单函数 - 未捕获的类型错误:无法读取未定义的属性“长度” - Javascript: Simple Function - Uncaught TypeError: Cannot read property 'length' of undefined 在函数内部 &gt;&gt;&gt; TypeError:无法读取未定义的属性“长度” - Inside the function >>> TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined TypeError无法读取未定义的属性“ length”-角度4 - TypeError cannot read property “length” of undefined - angular 4 opencv-TypeError:无法读取未定义的属性“长度” - opencv - TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“长度” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 开玩笑:TypeError:无法读取未定义的属性“长度” - Jest : TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM