for循环中的if-else无法正常工作-Javascript

时间:2019-07-07 05:08:33

标签: javascript arrays object for-loop if-statement

我有一个带有一些属性的数组“联系人”。我的函数使用for来检查每个联系人的firstName属性是否与我的函数的name参数匹配,如果匹配,它将检查该联系人是否具有与prop参数匹配的属性(如果在上一个if中,则为if)。这两个“ if”都有对应的“ elses”:“无此属性”,“无此联系”。

代码实际上非常简单:

var contacts = [
  {
    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]
  },
  {
    "firstName": "Harry",
    "lastName": "Potter",
    "number": "0994372684",
    "likes": ["Hogwarts", "Magic", "Hagrid"]
  },
  {
    "firstName": "Kristian",
    "lastName": "Vos",
    "number": "unknown",
    "likes": ["JavaScript", "Gaming", "Foxes"]
  }
];


function lookUpProfile(name, prop) {

  for (var i = 0; i < contacts.length; i++) {

    if (contacts[i].firstName === name) {

      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      }
      else {
        return "No such property";
      }
    }
    else {
      return "No such contact";}
  }  
}

console.log(lookUpProfile("Harry", "likes"));

以下几行很完美:

console.log(lookUpProfile("Akira", "likes"));
console.log(lookUpProfile("Akira", "lala"));

现在,如果我放置其余元素的其他firsNames:

console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Kristian", "likes"));

它返回“没有这样的联系”。...:/

2 个答案:

答案 0 :(得分:1)

那是因为您在第一次循环迭代中返回了No such contact。将此内容放在循环之后的函数底部。

var contacts=[{"firstName":"Akira","lastName":"Laine","number":"0543236543","likes":["Pizza","Coding","Brownie Points"]},{"firstName":"Harry","lastName":"Potter","number":"0994372684","likes":["Hogwarts","Magic","Hagrid"]},{"firstName":"Kristian","lastName":"Vos","number":"unknown","likes":["JavaScript","Gaming","Foxes"]}]

function lookUpProfile(name, prop) {

  for (var i = 0; i < contacts.length; i++) {

    if (contacts[i].firstName === name) {

      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    }
  }
  return "No such contact";
}

console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Kristian", "likes"));
console.log(lookUpProfile("Made Up Person", "likes"));
console.log(lookUpProfile("Akira", "Made Up Property"));

答案 1 :(得分:0)

在您当前的代码中,如果数组的第一个元素与当前传递的名称不匹配,则转到else块并返回“ No such contact”(最终仅检查第一个元素)

您从else块返回的for循环错误,您需要将其置于循环之外

var contacts = var contacts=[{"firstName":"Akira","lastName":"Laine","number":"0543236543","likes":["Pizza","Coding","Brownie Points"]},{"firstName":"Harry","lastName":"Potter","number":"0994372684","likes":["Hogwarts","Magic","Hagrid"]},{"firstName":"Kristian","lastName":"Vos","number":"unknown","likes":["JavaScript","Gaming","Foxes"]}]

function lookUpProfile(name, prop) {
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    }
  }
  return "No such contact";
}

console.log(lookUpProfile("Harry", "likes"));