JavaScript中的查找函数

eyh26e7m  于 2023-03-28  发布在  Java
关注(0)|答案(6)|浏览(114)

我在解决一个难题这是作业
在联系人列表中,我们有一个代表不同人的对象数组。
已经为您预先编写了一个lookUpProfile函数,它以firstName和一个属性(prop)作为参数。
该函数应该检查firstName是否是实际联系人的firstName,并且给定的属性(prop)是否是该联系人的属性。
如果两者都为真,则返回该属性的“值”。
如果firstName不对应于任何联系人,则返回“No such contact”
如果prop不对应于任何有效属性,则返回“No such property”
我的尝试

//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(“Donald”,“likes”);
〈.未定义
请帮助我解决这个问题。您也可以在this gist上发表评论
谢谢

toiithl6

toiithl61#

你在循环中的第一个条件应该检查name是否相等。如果它满足,那么你应该检查属性是否存在。如果它存在,则返回值else返回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"));
q8l4jmvw

q8l4jmvw2#

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

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

esyap4oy3#

尝试以下代码:

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

ekqde3dh4#

另一种方法是使用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”语句工作。

mo49yndu

mo49yndu5#

我自己在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
}
kuarbcqp

kuarbcqp6#

我在FreeCodeCamp的Javascript算法和数据结构配置文件查找中遇到了这个问题。

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) {
    const found = contacts.find(c => c.firstName == name)
    return !found ? "No such contact" : !found[prop] ? "No such property" : found[prop]
}

console.log(lookUpProfile("Akira", "address"));
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));

相关问题