简体   繁体   English

循环不会搜索整个对象数组吗?

[英]Loop won't search through entire array of object?

For javascript, I have an array of objects and I want to see if a user's entry matches 2 of the properties of any of the three objects in my array. 对于javascript,我有一个对象数组,我想查看用户的条目是否与数组中三个对象中任何一个的属性中的2个匹配。 For some reason my 'for loop' only works for the first object but never checks the other two. 由于某种原因,我的“ for循环”仅适用于第一个对象,而从不检查其他两个对象。 How can I fix this? 我怎样才能解决这个问题?

 class Customer { constructor(fN, lN, bal, cID, pass) { this.firstName = fN; this.lastName = lN; this.balance = bal; this.customerID = cID; this.password = pass; } } const bankers = []; bankers.push(new Customer("Jack", "Scott", 3689.21, "4552", "2811")); bankers.push(new Customer("John", "Smith", 2500.00, "4553", "1234")); bankers.push(new Customer("Albert", "Price", 100000.00, "4554", "6189")); let userID = prompt(`Please enter your customer ID.`); let userPass = prompt(`Please enter your password.`); for (let i = 0; i < bankers.length; i++) { if (bankers[i].customerID === userID && bankers[i].password === userPass) { console.log('Yay'); break; } else { console.log('boo'); break; } } 

My 'for loop' only works when I test for the first customer. 仅当我为第一个客户进行测试时,我的“ for循环”才有效。 If I try to enter a customerid or password for the other two it fails. 如果我尝试输入其他两个的客户ID或密码,它将失败。 Why is this happening? 为什么会这样呢? I thought the i variable should go through all 3 objects 我认为i变量应该遍历所有3个对象

Two things - one, you were missing a quote in front of Jack . 有两件事-一,您在Jack前面漏了一个引号。 Two, you need to redefine your variables each time your loop runs - move your userID and userPass declarations inside your loop: 二,你需要每个循环运行的时间来重新定义你的变量-将你的userID ,并userPass声明你的循环

 class Customer { constructor(fN, lN, bal, cID, pass) { this.firstName = fN; this.lastName = lN; this.balance = bal; this.customerID = cID; this.password = pass; } } const bankers = []; bankers.push(new Customer("Jack", "Scott", 3689.21, "4552", "2811")); bankers.push(new Customer("John", "Smith", 2500.00, "4553", "1234")); bankers.push(new Customer("Albert", "Price", 100000.00, "4554", "6189")); for (let i = 0; i < bankers.length; i++) { let userID = prompt(`Please enter your customer ID.`); let userPass = prompt(`Please enter your password.`); if (bankers[i].customerID === userID && bankers[i].password === userPass) { console.log('Yay'); } else { console.log('boo'); } } 

EDIT 编辑

Based on the comments, I believe you want to use some like so instead: 根据该意见,我相信你想使用some像这样,而不是:

 class Customer { constructor(fN, lN, bal, cID, pass) { this.firstName = fN; this.lastName = lN; this.balance = bal; this.customerID = cID; this.password = pass; } } const bankers = []; bankers.push(new Customer("Jack", "Scott", 3689.21, "4552", "2811")); bankers.push(new Customer("John", "Smith", 2500.00, "4553", "1234")); bankers.push(new Customer("Albert", "Price", 100000.00, "4554", "6189")); let userID = prompt(`Please enter your customer ID.`); let userPass = prompt(`Please enter your password.`); if (bankers.some(banker => banker.customerID == userID && banker.password == userPass)) { console.log('Yay'); } else { console.log('boo'); } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM