【问题标题】:Loop won't search through entire array of object?循环不会搜索整个对象数组?
【发布时间】:2019-03-07 01:41:57
【问题描述】:

对于 javascript,我有一个对象数组,我想查看用户的条目是否与我的数组中三个对象中的任何一个的属性中的 2 个匹配。出于某种原因,我的“for 循环”仅适用于第一个对象,但从不检查其他两个对象。我该如何解决这个问题?

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;
  }
}

我的“for 循环”仅在我为第一个客户测试时才有效。如果我尝试为其他两个输入客户 ID 或密码,则会失败。为什么会这样?我认为i 变量应该遍历所有 3 个对象

【问题讨论】:

  • @Ele 这是一个可能有害的编辑。您添加了报价。他们的描述表明这应该不是问题,但我们不确定。
  • 无法用调整后的代码重现。它能够很好地找到第二个条目。
  • @WaliH:我正要进行相同的编辑,只是没有缺少引号并在评论中指向它。
  • 我已经投票决定暂时关闭,因为问题中发布的代码有效。
  • 错字是我在这里粘贴我的问题时。我的现有代码中确实有引号

标签: javascript arrays function object ecmascript-6


【解决方案1】:

两件事 - 第一,您在 Jack 前面缺少引用。第二,您需要在每次循环运行时重新定义变量 - 将您的 userIDuserPass 声明移入您的循环:

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');
  }
}

编辑

基于 cmets,我相信您想像这样使用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');
  }

【讨论】:

  • 我的印象是他们想输入一个时间,并在此基础上进行查找,但意图也不是 100% 明确。
  • 将那些移到里面会有不同的行为。实际上(使用 Ele 的修复程序),代码会根据提示中的数据检查每个银行家。我认为这就是OP想要的。如果你把它们移到里面,你会提示每个庄家,并确保它在第 n 个位置……有点奇怪。
  • 是的,它只是一次输入
  • So @WaliH 您想检查 any banker 是否有用户 ID 和密码,并在该布尔值上记录 yayboo
  • 是的,我只是希望用户的条目能够匹配三个客户中任何一个的 ID 和密码
猜你喜欢
  • 2016-06-10
  • 2019-01-26
  • 2021-06-27
  • 1970-01-01
  • 2013-08-28
  • 2013-11-17
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多