繁体   English   中英

在 javascript 中查找 function

[英]Lookup function in javascript

我正在尝试解决一个挑战,这是问题集

我们有一组代表我们联系人列表中不同人的对象。

已经为您预先编写了一个 lookUpProfile function,它将 firstName 和属性 (prop) 作为 arguments。

function 应该检查 firstName 是否是实际联系人的 firstName 以及给定的属性 (prop) 是否是该联系人的属性。

如果两者都为真,则返回该属性的“值”。

如果 firstName 不对应任何联系人,则返回“No such contact”

如果 prop 不对应于任何有效的属性,则返回“没有这样的属性”

我的尝试

//Setup
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){
// Only change code below this line
for(var i =0;i<contacts.length;i++){
  if (contacts[i].firstName===firstName && contacts[i].hasOwnProperty(prop)===true){
    return contacts[i][prop];
  }
    else if (contacts[i].firstName===undefined){
return "No such contact";
    }
  else if (contacts[i].hasOwnProperty===false){
    return "No such property";
  }

}

// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

如果我在 lookUpProfile function 中输入不存在的值,它只会返回 undefined

例如 lookUpProfile("Donald", "likes");

<. 不明确的

请帮我解决这个问题。 您也可以对此要点发表评论

谢谢

循环中的第一个条件应该检查​​ name 是否相等。 如果它满足那么你应该检查属性是否存在如果它存在返回值否则返回no such property 。如果没有找到这样的名字那么它将退出循环并且你可以不返回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": "Sherlock", "lastName": "Holmes", "number": "0487345643", "likes": ["Intriguing Cases", "Violin"] }, { "firstName": "Kristian", "lastName": "Vos", "number": "unknown", "likes": ["Javascript", "Gaming", "Foxes"] } ]; function lookUpProfile(firstName, prop){ // Only change code below this line for(var i =0;i<contacts.length;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"; } //No such contact console.log(lookUpProfile("Donald", "likes")); //No such property console.log(lookUpProfile("Sherlock", "locks")); //returns value of property console.log(lookUpProfile("Kristian", "likes"));

你可以简单地像这样解决它:

const found = contacts.find(contact => contact.firstName == name)
return !found ? 'No such contact' : found[prop] || 'No such property'

尝试以下代码:

function lookUpProfile(firstName, prop){

    // Only change code below this line
    var isFirstName = false;
    for(var i =0;i<contacts.length;i++){
        if(contacts[i].firstName === firstName){
            isFirstName = true;
        }
    }
    if(!isFirstName){
        return "No such contact";
    }
    else {
        for(var i =0;i<contacts.length;i++){
            if (contacts[i].firstName===firstName && contacts[i].hasOwnProperty(prop)===true){
                return contacts[i][prop];
            }
            else if (contacts[i].hasOwnProperty===false){
                return "No such property";
            }
        }
    }

    // Only change code above this line
    }

    // Change these values to test your function
    console.log(lookUpProfile("Donald", "likes"))

另一种方法是使用 OR 运算符。 那么,只需要一个“if 语句”:

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 (i = 0; i < contacts.length; i++) {
        if (contacts[i].firstName === firstName) {
            return contacts[i][prop] || "No such property";
        }
            return "No such contact";
    }
}

console.log(lookUpProfile("Akira", "likes")); // Returns [ 'Pizza', 'Coding', 'Brownie Points' ]

console.log(lookUpProfile("Akira", "current job")); // Returns No such property

console.log(lookUpProfile("Karanja", "likes")); // Returns No such contact

在这里,OR 运算符的第二部分用作“else”语句。

我自己在 FreeCodeCamp 的Javascript 算法和数据结构配置文件查找问题上遇到了这个问题,上面的答案似乎有些过时了。

以下是我修复它的方法:

 function lookUpProfile(name, prop) { // Only change code below this line for (let i = 0; i < contacts.length; i++){ if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) { return contacts[i][prop] } else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) return "No such property" } return "No such contact" // Only change code above this line }

暂无
暂无

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

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