我有以下对象数组:
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"]
}
];
我想定义一个函数lookUpProfile(firstName, prop)
,该函数用于输入数据:
lookUpProfile("Akira", "likes");
将返回:
["Pizza", "Coding", "Brownie Points"]
到目前为止,这是我的解决方案,直到现在还没有回复:
function lookUpProfile(firstName, prop){
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName == firstName){
for(var j = 0; j < contacts[i].length; j++){
if(contacts[i].hasOwnProperty(prop))
return prop;
}
}
}
}
有没有人想解决这个问题?感谢
答案 0 :(得分:3)
您可以尝试这样做。找到包含.find()
函数( ES6 )的项目,并使用括号[]
语法返回相应的属性。
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 lookUpProfileES6(name, prop){
var found = contacts.find(item => item.firstName === name);
if(found){
return found[prop];
}
}
function lookUpProfileES5(name, prop){
var found = null;
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName === name){
found = contacts[i];
break;
}
}
if(found){
return found[prop];
}
}
var resultES6 = lookUpProfileES6("Akira", "likes");
var resultES5 = lookUpProfileES5("Akira", "likes");
console.log(resultES6);
console.log(resultES5);
答案 1 :(得分:1)
你的问题是你试图用带有数字indeces的for循环遍历对象。你根本不需要第二个循环。它可以像
一样简单function lookUpProfile(firstName, prop) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
}
}
}
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) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop))
return contacts[i][prop];
}
}
}
console.log(lookUpProfile("Akira", "likes"))
答案 2 :(得分:1)
您的代码存在问题:
for(var j = 0; j < contacts[i].length; j++){
此处无需再次尝试迭代。此外,contacts [i]是一个对象,因此没有长度属性,因此您要比较始终为false的0 < undefined
。
这会导致您对属性的条件检查从未发生过。除此之外,prop是这里的访问者。因此,虽然它可能包含属性名称,但它不包含属性值。为了正确返回属性值,您需要在当前对象上以[prop]
的形式将其用作访问器。
删除第二次迭代,正确访问联系对象,您的功能将正常工作。
function lookUpProfile(firstName, prop){
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName == firstName){
if(contacts[i].hasOwnProperty(prop))
return contacts[i][prop];
}
}
}
答案 3 :(得分:1)
你可以这样做
// Create a function which will filter out the requirement
var filtered = function(name, likes) {
var _toRet = ''
contacts.filter(function(elem, index) {
// check if the first name is same as requires
// check if the object have property likes
// Since the requirement is to search the likes array
if (elem.firstName === name && elem.hasOwnProperty(likes)) {
_toRet = elem[likes]
}
return _toRet // return from filter callback
})
return _toRet // return from the function
}
console.log(filtered('Akira', 'likes'))