简体   繁体   English

无法读取未定义的属性“长度”。”

[英]Cannot read property 'length' of undefined."

This program should print out the arrays.这个程序应该打印出 arrays。

But why am I getting但为什么我得到

Cannot read property 'length' of undefined.无法读取未定义的属性“长度”。

I am new to learning JavaScript.我是学习 JavaScript 的新手。 Thank you.谢谢你。

 const animearray= [ ["mursalin",'tuly','ahad'], ['eren','luffy','minato'], ['asif','sara','rakib'] ] for (let i=0;i<=animearray.length;i++){ const rows = animearray[i]; for (let j=0;j<=rows.length; j++){ console.log(rows[j]); } }

for (let i=0;i<=animearray.length;i++)

Since arrays are indexed from 0 to length-1 , where length is the number of elements in the array, the program is throwing an error when i equals the array length.由于 arrays 的索引从0length-1 ,其中length是数组中元素的数量,因此当i等于数组长度时程序会抛出错误。 Iterating upto animearray.length-1 would solve the problem.迭代到animearray.length-1可以解决这个问题。

 const animearray = [ ["mursalin", 'tuly', 'ahad'], ['eren', 'luffy', 'minato'], ['asif', 'sara', 'rakib'] ] animearray.forEach(function(e) { e.forEach(function(a) { console.log(a); }); });

for (let i = 0; i < animearray.length; i++) {
  const rows = animearray[i];
  for (let j = 0; j < rows.length; j++) {
    console.log(rows[j]);
  }
}

but if you want to loop through array or multidimensional array then you can use forEach loop also但是如果你想遍历数组或多维数组,那么你也可以使用 forEach 循环


    animearray.forEach(function (e) {
      e.forEach(function (a) {
        console.log(a);
      });
    });

You are iterating for an extra element you have initialized the I=0 and loop is running 4 times which means checking 0,1,2,3 where on 3rd iteration it is not finding anything您正在迭代一个额外的元素,您已初始化I=0并且循环运行4 次,这意味着检查0、1、2、3 ,在第 3 次迭代中它没有找到任何东西

 const animearray= [ ["mursalin",'tuly','ahad'], ['eren','luffy','minato'], ['asif','sara','rakib'], ] for (let i=0;i<=animearray.length-1;i++){ const rows = animearray[i]; for (let j=0;j<=rows.length-1; j++){ console.log(rows[j]); } }

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

相关问题 现有数据数组:无法读取未定义的属性长度。 的JavaScript - Existing Array with data: Cannot read property length of undefined. JavaScript 无法读取未定义的属性“ removeClass”。 Java脚本 - Cannot read property “removeClass” of Undefined. Javascript 无法读取未定义的属性。 如何定义? - Cannot read property of undefined. How to define? “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 当我尝试制作 ID 时; “无法读取未定义的属性‘长度’。” - When I try to make ID ; "Cannot read property 'length' of undefined." 无法读取未定义的属性“indexOf”。 为什么是未定义的? - Cannot read property 'indexOf' of undefined. Why is it undefined? 无法读取未定义的属性。 但属性是进口 class 的 static function - Cannot read property of undefined. But property is a static function of an imported class 无法从未定义中读取属性“ 0”。 错误的Google Apps脚本 - Cannot read property “0” from undefined. Error Google Apps Script TypeError:无法读取未定义的属性“ get”。 是npm的问题吗? - TypeError: Cannot read property 'get' of undefined. Is npm the issue? 无法读取未定义的属性“ x”。 json文件中的第二级 - Cannot read property 'x' of undefined. second level in json file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM