为了账号安全,请及时绑定邮箱和手机立即绑定

Javascript 配置文件查找

Javascript 配置文件查找

ABOUTYOU 2023-03-18 17:43:57
我正在尝试解决如下所示的以下 Javascript 问题。我一直在试图找出它的哪一部分失败了,并且已经失败了一段时间。我确实有初始教程提供的解决方案,但不清楚为什么这不太有效。我们有一个代表我们联系人列表中不同人的对象数组。已经为您预先编写了一个以名称和属性 (prop) 作为参数的 lookUpProfile 函数。该函数应检查 name 是否为实际联系人的 firstName 以及给定属性 (prop) 是否为该联系人的属性。如果两者都为真,则返回该属性的“值”。如果名称不对应于任何联系人,则返回“没有这样的联系人”。如果 prop 不对应于找到的与名称匹配的联系人的任何有效属性,则返回“没有这样的属性”。// Setupvar 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){// Only change code below this linefor (var i=0; i<contacts.length; i++){ for (var j=0; j<contacts[i].length; j++){if(contacts[i][0]===name&&contacts[i][j]===prop){ return contacts[i][j]    ;}else if (contacts[i]===name) {return "No such property";}else return "No such contact";}   }// Only change code above this line}lookUpProfile("Akira", "likes");
查看完整描述

2 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

我认为这是让你感到困惑的 for 循环。问题要求我们检查是否name does not correspond to any contacts then return "No such contact"。因此,在您的回答中,您一到达第一个无效联系人就

返回,这不是想要的问题。 您应该将自己排除在 for 循环之外,作为最终的包罗万象。else if (contacts[i]===name) {return "No such property";}

return "No such contact"


除此之外,当您需要检查对象中是否存在某个属性时,您可以使用Object.hasOwnProperty(propertyName). contacts[i]inelse if (contacts[i]===name) {return "No such property";}会给你联系人中的实际对象,它永远不会匹配name.

您将需要获得name带括号或点的注释。例子:


contacts[0] is equal to

{

    "firstName": "Akira",

    "lastName": "Laine",

    "number": "0543236543",

    "likes": ["Pizza", "Coding", "Brownie Points"]

}

contacts[0].firstName is equal to "Akira"

contacts[0]["firstName"] is equal to "Akira"


// 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(name, prop) {

    // Only change code below this line

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

        const contact = contacts[i];

        if (contact.firstName === name && contact.hasOwnProperty(prop)) {

            return contact[prop];

        } else if (contact.firstName === name && !contact.hasOwnProperty(prop)) {

            return "No such property";

        }

    }

    return "No such contact";

    // Only change code above this line

}


lookUpProfile("Akira", "likes");


查看完整回答
反对 回复 2023-03-18
?
MMTTMM

TA贡献1869条经验 获得超4个赞

先找个人资料,再找房产


function lookUpProfile(name, prop) {

  const profile = contacts.find((contact) => contact.firstName === name)

  if (!profile) return "No such contact"


  const property = profile[prop]

  if (!property) return "No such property"


  return 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(name, prop) {

  // Only change code below this line

  const profile = contacts.find((contact) => contact.firstName === name)

  if (!profile) return "No such contact"


  const property = profile[prop]

  if (!property) return "No such property"


  return property

  // Only change code above this line

}


console.log(lookUpProfile("Akira", "likes"))

console.log(lookUpProfile("Bkira", "likes"))

console.log(lookUpProfile("Akira", "nikes"))


查看完整回答
反对 回复 2023-03-18
  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信