简体   繁体   English

在 JS 中迭代我的数组,但出现“无法读取未定义的属性‘长度’”错误

[英]Iterating on my array in JS but I'm getting a "Cannot read property 'length' of undefined" error

I am trying to build a simple zip code checker where the program will check a user's input zip code matches a list of valid zip codes.我正在尝试构建一个简单的邮政编码检查器,该程序将检查用户输入的邮政编码是否与有效邮政编码列表相匹配。 The issue is, I am running into an error that says "Cannot read property 'length' of undefined".问题是,我遇到了一个错误,提示“无法读取未定义的属性‘长度’”。 What am I doing wrong?我究竟做错了什么?

 let validZips = [12345, 78910]; let zip = document.getElementById("zipCode").value; const checkCode = (zip, validZips) => { for (let i = 0; i < validZips.length; i++) { if (zip !== validZips[i]) { alert("out of service area") } } }
 <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> </head> <body> <div class="container"> <label for="zipCode">Zip Code</label> <br> <input type="text" placeholder="Enter a zipcode" id="zipCode"> <br> <button type="submit" onclick="checkCode()"> Check Zip Code</button> </div> </body> </html>

validZips.length works fine, check this out validZips.length 工作正常,看看这个

 let validZips = [12345, 78910]; const checkCode = (zip, validZips) => { for (let i = 0; i < validZips.length; i++) { if (zip !== validZips[i]) { console.log("out of service area, ", validZips[i]); } } } checkCode(1234, validZips);

User "barzin.A" suggested that the code wasn't executing because the array wasn't in the function scope.用户“barzin.A”建议代码没有执行,因为数组不在函数范围内。 I defined the array in the scope of the function and everything worked as intended.我在函数范围内定义了数组,一切都按预期工作。 Here's the solution code:这是解决方案代码:

let zip = document.getElementById("zipCode").value;

 const checkCode = (validZips,zip) =>

 {
    validZips = [12345,67891];
     for(let i=0; i < validZips.length; i++){

     if (zip !== validZips[i]) {
         alert("out of service area")
     }
 }

暂无
暂无

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

相关问题 我不知道为什么我在这里得到“无法读取未定义的属性&#39;长度&#39;的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 我的程序不断收到此错误:VM178 SortLib.js:71 Uncaught TypeError: Cannot read property &#39;length&#39; of undefined - I keep getting this error for my program: VM178 SortLib.js:71 Uncaught TypeError: Cannot read property 'length' of undefined 收到此错误:无法读取未定义的属性“长度” - Getting this error: Cannot read property 'length' of undefined 我在我的 React 项目中收到此错误“TypeError: Cannot read property 'map' of undefined” - I'm getting this error “TypeError: Cannot read property 'map' of undefined” on my React project 尝试 map 我的道具时,我收到无法读取未定义错误的“练习”属性 - I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props 为什么在 pug 模板中进行迭代时会出现“无法读取未定义的属性‘长度’”错误? - Why do I get a "Cannot read property 'length' of undefined" error when iterating in a pug template? 我收到了Uncaught TypeError:无法在jquery.main.js上读取null的属性“ length” - I'm getting an Uncaught TypeError: Cannot read property 'length' of null on jquery.main.js 出现错误:无法读取未定义的属性“长度” - Getting Error regarding :Cannot read property 'length' of undefined 获取错误:“未捕获的类型错误:无法读取未定义的属性‘长度’” - Getting error: "Uncaught TypeError: Cannot read property 'length' of undefined" Select2.js错误:无法读取未定义的属性“长度” - Select2.js error: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM