我似乎无法从多维数组中获取所需的记录。它并没有给我任何特定的错误,让我准确地指出我做错了什么。
你能帮忙吗?
//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
var i = 0;
while(i < contacts.length) {
if (contacts[i].firstName === firstName) {
return contacts[i].prop;
}
i++;
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
更改
return contacts[i].prop;
至
return contacts[i][prop];
扩展一下:在您的原始代码中,您要求从联系人获取名为"prop"的字段。您的意图是想要具有变量prop
的值的名称的字段。
将contacts[i].prop;
更改为contacts[i][prop];