我正在使用 freeCodeCamp javascript 并陷入“配置文件查找”练习,因为我忘记了 .hasOwnProperty() 函数,但我仍然不确定为什么我的原始函数不起作用。我将保留给定数组的一部分以供参考。
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
};
function lookUpProfile(name, prop){
// Only change code below this line
for(let x in contacts){
if(name === contacts[x].firstName){
for(let y in contacts[x]){
if(prop === y){
return contacts[x][prop];
} else {return "No such property";}
}
}
} return "No such contact";
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes")
当我离开我的
else {return "No such property";}
行它可以工作,但无论“prop”输入是什么,否则只会返回“没有这样的属性”。