从Javascript对象中检索属性

时间:2017-06-12 08:22:58

标签: javascript

以下代码。以下返回正确的结果:

lookUpProfile("Bob", "number"); // returns 'No such contact'
lookUpProfile("Akira", "address"); // return 'No such property'

但是,对于以下情况,它不会返回预期结果。

lookupProfile("Kristian", "lastName"); // should return 'Vos'
lookupProfile("Sherlock", "likes"); // should return ["Intriguing Cases", "Violin"]

谢谢。

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) {
      if (contacts[i][prop] === prop) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    } else {
      return "No such contact";
    } 
  }
// Only change code above this line
}

3 个答案:

答案 0 :(得分:3)

您正试图像contacts[i].prop

那样访问该媒体资源

prop是一个变量,因此您需要使用括号表示法contacts[i][prop]

另一种方法:

您可以使用Array#find()与名字联系。

如果找不到联系人,

find()将返回undefined。他们可以访问该属性,看看它是否返回undefined

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) {
  const contact = contacts.find(c=>c.firstName === firstName);
  if(!contact) return "No such contact";
  const propValue = contact[prop];
  if(!propValue) return "No such property";
  return propValue;
}

console.log(lookUpProfile("Bob", "number")); // returns 'No such contact'
console.log(lookUpProfile("Akira", "address")); // return 'No such property'
console.log(lookUpProfile("Kristian", "lastName")); // should return 'Vos'
console.log(lookUpProfile("Sherlock", "likes")); // should return ["Intriguing Cases", "Violin"]

并且带着单线回归

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) {
  const contact = contacts.find(c=>c.firstName === firstName);
  return contact ? contact[prop] ? contact[prop] : "No such property" : "No such contact";
}

console.log(lookUpProfile("Bob", "number")); // returns 'No such contact'
console.log(lookUpProfile("Akira", "address")); // return 'No such property'
console.log(lookUpProfile("Kristian", "lastName")); // should return 'Vos'
console.log(lookUpProfile("Sherlock", "likes")); // should return ["Intriguing Cases", "Violin"]

使用for-loop

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(let i=0; i<contacts.length; i++){
    if(contacts[i].firstName === firstName){
      if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }else{
        return "No such property";
      }
    }
  }
  return "No such contact";
}

console.log(lookUpProfile("Bob", "number")); // returns 'No such contact'
console.log(lookUpProfile("Akira", "address")); // return 'No such property'
console.log(lookUpProfile("Kristian", "lastName")); // should return 'Vos'
console.log(lookUpProfile("Sherlock", "likes")); // should return ["Intriguing Cases", "Violin"]

答案 1 :(得分:1)

获取道具的正确代码:

return contacts[i][prop]

当你contacts[i].prop时,它正在寻找{'prop':...}

相应的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

答案 2 :(得分:0)

for (var key in contacts) {
  //You can loop through the contacts object
  var obj = contacts[key];//get the object
}