javascript中的查找功能

时间:2017-02-16 08:21:01

标签: javascript

我试图解决一个挑战,而这就是问题集

  

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

     

一个lookUpProfile函数,它将firstName和一个属性(prop)作为   已为您预先写好了论据。

     

该函数应检查firstName是否为实际联系人   firstName和给定属性(prop)是该联系人的属性。

     

如果两者都为真,则返回"值"该财产。

     

如果firstName与任何联系人不对应,则返回"不这样   接触"

     

如果道具与任何有效的属性不对应,则返回"否   这样的财产"

我的尝试

//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函数中输入不存在的值,它只返回undefined

例如lookUpProfile(&#34; Donald&#34;,&#34;喜欢&#34;);

&LT ;.未定义

请帮我解决这个问题。 您也可以对this gist

发表评论

由于

4 个答案:

答案 0 :(得分:1)

循环中的第一个条件应检查名称是否相等。如果它满足那么你应该检查属性是否存在如果它存在则返回值,否则返回no such property。如果没有找到这样的名称,那么它将退出循环,你可以返回no such contact

&#13;
&#13;
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"));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

请尝试以下代码:

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"))

答案 3 :(得分:0)

执行此操作的另一种方法是使用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运算符的第二部分用作“其他”语句。