我似乎无法从多维数组中获取我需要的记录。并没有给我任何特别的错误,可以让我明确指出我做错了什么。
你能帮忙吗?
//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");
答案 0 :(得分:1)
更改
return contacts[i].prop;
到
return contacts[i][prop];
要扩展一下:在原始代码中,您要求获得名为&#34; prop&#34;的字段。来自联系。您的意图是希望字段具有变量prop
的值。
答案 1 :(得分:1)
将contacts[i].prop;
更改为contacts[i][prop];