经典案例获取数组里的对象是否匹配
Posted 祁祁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了经典案例获取数组里的对象是否匹配相关的知识,希望对你有一定的参考价值。
//初始化变量
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 lookUp(firstName, prop){
// 请把你的代码写在这条注释以下
for(var i=0;i<contacts.length;i++){
if(contacts[i].firstName=== firstName){
if(contacts[i][prop]){
return contacts[i][prop];
}
return "No such property";
}
}
return "No such contact";
// 请把你的代码写在这条注释以上
}
// 你可以修改这一行来测试你的代码
lookUp("Akira", "likes");
以上是关于经典案例获取数组里的对象是否匹配的主要内容,如果未能解决你的问题,请参考以下文章