我試圖重構這個函數,但已經撞牆了。函數試圖完成的是查找JSON對象中是否存在聯繫人,如果存在,請確定提供的第二個參數是否是存在於指定對象上的屬性。重構查找函數
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)) {
return contacts[i][prop];
}
}
for (var j = 0; j < contacts.length; j++) {
if (contacts[j].firstName !== firstName) {
return "No such contact";
} else if (!contacts[j].hasOwnProperty(!prop)) {
return "No such property" ;
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("kyle", "lastName");
會不會有永遠只能是一個接觸的比賽,或許多返回
true
? – Andy