简体   繁体   English

为什么我收到此错误?Uncaught TypeError:无法读取未定义的属性“ length”

[英]why I am getting this error Uncaught TypeError: Cannot read property 'length' of undefined

I want sort this sentence "This account has an unconfirmed email address" by word length how can i do this. 我想按字长对“该帐户的电子邮件地址未经确认”这句话进行排序,我该怎么做。

<script>
$(document).ready(function(){
var some = "";
xx  = "";
 var vText = "This account has an unconfirmed email address";
    slitTxt = vText.split(" ");
    for(i=0; i <= slitTxt.length; i++ ){
        xx = slitTxt[i].length;
        some = $('body').append(xx+" ")

    }


 });
 </script>

you are checking for lastindex+1 that does'nt exist or is undefined, remove equals to sign from loop condition replace "<=" with "<" 您正在检查不存在或未定义的lastindex + 1,请从循环条件中删除等于符号以将“ <=”替换为“ <”

for example if array length is 5 using <= array.length means that check for 5 or less than 5 ,here first index is 0 and last is 4 so 5 is undefined 例如,如果使用<= array.length的数组长度为5,则表示检查5或小于5,这里第一个索引为0,最后一个索引为4,所以5是未定义的

<script>
$(document).ready(function(){
var some = "";
xx  = "";
 var vText = "This account has an unconfirmed email address";
    slitTxt = vText.split(" ");
    for(i=0; i < slitTxt.length; i++ ){
        xx = slitTxt[i].length;
        some = $('body').append(xx+" ")

    }


 });
 </script>

The error come because of this line : 由于此行而导致错误:

for(i=0; i <= slitTxt.length; i++ )

An array is 0-indexed, which mean the first cell is 0 but the length would be 1 数组的索引为0,这意味着第一个单元格为0但长度为1

So if you array contain 5 elements, the last one will be arr[4] or arr[arr.length-1] 因此,如果数组包含5个元素,则最后一个将是arr[4]arr[arr.length-1]

So when your condition in the for loop is i <= slitTxt.length , the last iteration will be the same as slitTxt[slitTxt.length] , hence, it will be undefined . 因此,当您在for循环中的条件是i <= slitTxt.length ,最后一次迭代将与slitTxt[slitTxt.length]相同,因此它将是undefined

Try that instead : 尝试尝试:

for(i=0; i < slitTxt.length; i++ )

You can use map() instead of for loop. 您可以使用map()代替for循环。 Example: 例:

 var vText = "This account has an unconfirmed email address", slitTxt = vText.split(" "); slitTxt.map(function(val, ind){$('body').append(val + " ");}); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <html><body></body></html> 

暂无
暂无

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

相关问题 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标题'”? - Why am i getting this error: “Uncaught TypeError: Cannot read property 'Title' of undefined”? 为什么我会收到“未捕获的类型错误:无法读取未定义的属性 &#39;body&#39;”? - Why am I getting "Uncaught TypeError: Cannot read property 'body' of undefined"? 我收到一个错误 - 未捕获的类型错误:无法读取未定义的属性“indexOf” - I am getting an error- Uncaught TypeError: Cannot read property 'indexOf' of undefined jQuery错误:未捕获的TypeError:无法读取未定义的属性“长度” - Jquery error: Uncaught TypeError: Cannot read property 'length' of undefined jQuery回调错误:未捕获的TypeError:无法读取未定义的属性“长度” - Jquery callback error: Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM