[英]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 的索引从0到length-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.