Javascript 配置文件查找

我正在尝试解决如下所示的以下 Javascript 问题。我一直在试图找出它的哪一部分失败了,并且已经失败了一段时间。我确实有初始教程提供的解决方案,但不清楚为什么这不太有效。


我们有一个代表我们联系人列表中不同人的对象数组。已经为您预先编写了一个以名称和属性 (prop) 作为参数的 lookUpProfile 函数。该函数应检查 name 是否为实际联系人的 firstName 以及给定属性 (prop) 是否为该联系人的属性。如果两者都为真,则返回该属性的“值”。如果名称不对应于任何联系人,则返回“没有这样的联系人”。如果 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(name, prop){

// Only change code below this line

for (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");


ABOUTYOU
浏览 118回答 2
2回答

慕码人8056858

我认为这是让你感到困惑的 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"// 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 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");

MMTTMM

先找个人资料,再找房产function lookUpProfile(name, prop) {&nbsp; const profile = contacts.find((contact) => contact.firstName === name)&nbsp; if (!profile) return "No such contact"&nbsp; const property = profile[prop]&nbsp; if (!property) return "No such property"&nbsp; return property}// Setupvar contacts = [&nbsp; {&nbsp; &nbsp; firstName: "Akira",&nbsp; &nbsp; lastName: "Laine",&nbsp; &nbsp; number: "0543236543",&nbsp; &nbsp; likes: ["Pizza", "Coding", "Brownie Points"],&nbsp; },&nbsp; {&nbsp; &nbsp; firstName: "Harry",&nbsp; &nbsp; lastName: "Potter",&nbsp; &nbsp; number: "0994372684",&nbsp; &nbsp; likes: ["Hogwarts", "Magic", "Hagrid"],&nbsp; },&nbsp; {&nbsp; &nbsp; firstName: "Sherlock",&nbsp; &nbsp; lastName: "Holmes",&nbsp; &nbsp; number: "0487345643",&nbsp; &nbsp; likes: ["Intriguing Cases", "Violin"],&nbsp; },&nbsp; {&nbsp; &nbsp; firstName: "Kristian",&nbsp; &nbsp; lastName: "Vos",&nbsp; &nbsp; number: "unknown",&nbsp; &nbsp; likes: ["JavaScript", "Gaming", "Foxes"],&nbsp; },]function lookUpProfile(name, prop) {&nbsp; // Only change code below this line&nbsp; const profile = contacts.find((contact) => contact.firstName === name)&nbsp; if (!profile) return "No such contact"&nbsp; const property = profile[prop]&nbsp; if (!property) return "No such property"&nbsp; return property&nbsp; // Only change code above this line}console.log(lookUpProfile("Akira", "likes"))console.log(lookUpProfile("Bkira", "likes"))console.log(lookUpProfile("Akira", "nikes"))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript