为什么在我更新了条件语句后,我的函数不能识别数组中的一个值?

huangapple go评论64阅读模式
英文:

Why doesn't my function recognize a value in an array after I update my if statement?

问题

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 if (contacts[i].firstName[name] === undefined) {
      return "No such contact";
    }
  } 
}
英文:

I'm trying to get the data of a contact when the name actually exist in my array and when it doesn't an "No such contact" prompt, and like this it works and I get "Vos"

const contacts = [
  {
    firstName: &quot;Akira&quot;,
    lastName: &quot;Laine&quot;,
    number: &quot;0543236543&quot;,
    likes: [&quot;Pizza&quot;, &quot;Coding&quot;, &quot;Brownie Points&quot;],
  },
  {
    firstName: &quot;Harry&quot;,
    lastName: &quot;Potter&quot;,
    number: &quot;0994372684&quot;,
    likes: [&quot;Hogwarts&quot;, &quot;Magic&quot;, &quot;Hagrid&quot;],
  },
  {
    firstName: &quot;Sherlock&quot;,
    lastName: &quot;Holmes&quot;,
    number: &quot;0487345643&quot;,
    likes: [&quot;Intriguing Cases&quot;, &quot;Violin&quot;],
  },
  {
    firstName: &quot;Kristian&quot;,
    lastName: &quot;Vos&quot;,
    number: &quot;unknown&quot;,
    likes: [&quot;JavaScript&quot;, &quot;Gaming&quot;, &quot;Foxes&quot;],
  },
];

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

lookUpProfile(&quot;Kristian&quot;, &quot;lastName&quot;);

But when I add and extra else if it just doesn't recognize the name any more and I get the "No such contact". I guess is something really simple and kind of obvious, but I just don't get why

const contacts = [
  {
    firstName: &quot;Akira&quot;,
    lastName: &quot;Laine&quot;,
    number: &quot;0543236543&quot;,
    likes: [&quot;Pizza&quot;, &quot;Coding&quot;, &quot;Brownie Points&quot;],
  },
  {
    firstName: &quot;Harry&quot;,
    lastName: &quot;Potter&quot;,
    number: &quot;0994372684&quot;,
    likes: [&quot;Hogwarts&quot;, &quot;Magic&quot;, &quot;Hagrid&quot;],
  },
  {
    firstName: &quot;Sherlock&quot;,
    lastName: &quot;Holmes&quot;,
    number: &quot;0487345643&quot;,
    likes: [&quot;Intriguing Cases&quot;, &quot;Violin&quot;],
  },
  {
    firstName: &quot;Kristian&quot;,
    lastName: &quot;Vos&quot;,
    number: &quot;unknown&quot;,
    likes: [&quot;JavaScript&quot;, &quot;Gaming&quot;, &quot;Foxes&quot;],
  },
];

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

lookUpProfile(&quot;Kristian&quot;, &quot;lastName&quot;);

答案1

得分: 0

问题在于在第二段代码中,在第一次迭代后就退出了循环。因此,你只检查了第一个项目,然后返回'没有这样的联系人'。看起来你只想在找不到联系人时才在最后返回。我添加了一个控制台日志,显示了循环经过了多少次迭代,这应该有助于故障排除。

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

function lookUpProfile(name, prop) {
  for (var i = 0; i < contacts.length; i++) {
    console.log('循环: ' + i);
    if (contacts[i].firstName === name) {
        if (contacts[i].hasOwnProperty(prop)) {
          return contacts[i][prop];
        } else {
          return "没有这样的属性";
        }
    } else if (contacts[i].firstName[name] === undefined) {
        return "没有这样的联系人";
    }
  } 
}

console.log(lookUpProfile("Kristian", "lastName"));
英文:

The problem is you are returning from the loop after the first iteration in the second snippet. As such, you are only checking the first item and then returning 'No such contact'. It looks like you only want to return at the very end if not contact has been found. I have added a console log that shows how many iteration the loop goes through, which should help with troubleshooting.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const contacts = [
  {
    firstName: &quot;Akira&quot;,
    lastName: &quot;Laine&quot;,
    number: &quot;0543236543&quot;,
    likes: [&quot;Pizza&quot;, &quot;Coding&quot;, &quot;Brownie Points&quot;],
  },
  {
    firstName: &quot;Harry&quot;,
    lastName: &quot;Potter&quot;,
    number: &quot;0994372684&quot;,
    likes: [&quot;Hogwarts&quot;, &quot;Magic&quot;, &quot;Hagrid&quot;],
  },
  {
    firstName: &quot;Sherlock&quot;,
    lastName: &quot;Holmes&quot;,
    number: &quot;0487345643&quot;,
    likes: [&quot;Intriguing Cases&quot;, &quot;Violin&quot;],
  },
  {
    firstName: &quot;Kristian&quot;,
    lastName: &quot;Vos&quot;,
    number: &quot;unknown&quot;,
    likes: [&quot;JavaScript&quot;, &quot;Gaming&quot;, &quot;Foxes&quot;],
  },
];

function lookUpProfile(name, prop) {
  for (var i = 0; i &lt; contacts.length; i++) {
    console.log(&#39;Loop: &#39; + i);
    if (contacts[i].firstName === name) {
        if (contacts[i].hasOwnProperty(prop)) {
          return contacts[i][prop];
        } else {
          return &quot;No such property&quot;
        }
    } else if (contacts[i].firstName[name] === undefined) {
        return &quot;No such contact&quot;
    }
  } 
}

console.log(lookUpProfile(&quot;Kristian&quot;, &quot;lastName&quot;));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 02:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定