简体   繁体   English

基本配置文件查找总是返回未找到联系人

[英]Basic Profile Lookup always returning no contact found

So I've recently begun learning Javascript using the tutorials on freecodecamp and there's this challenge I've been stuck on for a few hours now.所以我最近开始使用 freecodecamp 上的教程学习 Javascript,这个挑战我已经坚持了几个小时。

The function always returns 'No contact found' and I don't understand why.该函数总是返回“未找到联系人”,我不明白为什么。 If someone were to explain it to me and correct my code, I'd be grateful.如果有人向我解释并更正我的代码,我将不胜感激。

var 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(firstName, prop) {

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

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

lookUpProfile("Akira", "lastName");

Better use Array.prototype.find() for this:最好为此使用Array.prototype.find()

function findByProp(list, val, prop) {
  let found = list.find(el => el[prop] === val);
  return found ? found[prop] : "No contact found";
}

 var 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 findByProp(list, val, prop) { let found = list.find(el => el[prop] === val); return found? found[prop]: "No contact found"; } console.log(findByProp(contacts, "Akira", "firstName")); console.log(findByProp(contacts, "foo", "firstName"));

Try this试试这个

Explanation解释

  1. Typo error change like this contacts[i]['firstName'] instead of contacts[i][firstName] .you are missing to match the keyname of obj .for your way its calling like拼写错误更改为 contacts[i]['f keyname contacts[i]['firstName']而不是contacts[i][firstName] 。您缺少匹配obj的键名。对于您的调用方式

    contacts[i][Akira] == false statement so only it always go else statement contacts[i][Akira] == false statement所以只有它总是去 else 语句

  2. Do the object key call method with obj[key] instead of obj.key .Beacuse all are varible not with direct name of the key使用obj[key]而不是obj.key执行对象键调用方法。因为所有都是变量而不是键的直接名称

  3. second one hasownproperty(varible) .you are not mention which word to check with that object第二个hasownproperty(varible)你没有提到要用那个对象检查哪个词

 var 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(firstName, prop) { for (var i=0; contacts.length>i; i++) { if (contacts[i]['firstName']==firstName) { if (contacts[i].hasOwnProperty(prop) === true) { return contacts[i][prop]; } else { return "No such property"; } } return "No such contact"; } } console.log(lookUpProfile("Akira", "lastName"));

function lookUpProfile(name, prop) {

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

        if ((name == contacts[i].firstName)) {
            if (contacts[i].hasOwnProperty(prop)) {
                return contacts[i][prop];
            } else
                return "No such property";
        }
        
        if (i == contacts.length - 1) return "No such contact";
    }
}

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

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