最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript: Returning a property of an object - Stack Overflow

programmeradmin1浏览0评论

I am doing a challenge from freeCodeCamp called "Profile Lookup". My problem seems to be that I cannot return the value of a property within an object. I am using dot notation, but have also tried using bracket notation.

The objective is to create a function that loops through an array of objects, contacts, to find whether a given firstName is within said array and whether that object contains a given prop.

If both the firstName and the prop exist, we have to return the value of the property.

If the firstName is found but prop is not, then we return "No such property".

If firstName is not found, we return "No such contact".

This is my code:

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].hasOwnProperty(prop))
          return contacts.prop; 
        else 
          return "No such property"; 
      }
      else
        return "No such contact"; 
    }

// Only change code above this line
}

This is the array of objects that it is supposed to loop through:

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"]
    }
]; 

EDIT: I must have dropped the [i] in return contacts[i].prop when I changed to bracket notation and then back to dot notation. Even when I reapply it, I am getting the same problem. Here is the updated function:

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].hasOwnProperty(prop))
          return contacts[i].prop; 
        else 
          return "No such property"; 
      }
      else
        return "No such contact"; 
    }

// Only change code above this line
}

I am doing a challenge from freeCodeCamp called "Profile Lookup". My problem seems to be that I cannot return the value of a property within an object. I am using dot notation, but have also tried using bracket notation.

The objective is to create a function that loops through an array of objects, contacts, to find whether a given firstName is within said array and whether that object contains a given prop.

If both the firstName and the prop exist, we have to return the value of the property.

If the firstName is found but prop is not, then we return "No such property".

If firstName is not found, we return "No such contact".

This is my code:

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].hasOwnProperty(prop))
          return contacts.prop; 
        else 
          return "No such property"; 
      }
      else
        return "No such contact"; 
    }

// Only change code above this line
}

This is the array of objects that it is supposed to loop through:

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"]
    }
]; 

EDIT: I must have dropped the [i] in return contacts[i].prop when I changed to bracket notation and then back to dot notation. Even when I reapply it, I am getting the same problem. Here is the updated function:

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].hasOwnProperty(prop))
          return contacts[i].prop; 
        else 
          return "No such property"; 
      }
      else
        return "No such contact"; 
    }

// Only change code above this line
}
Share Improve this question edited Jun 19, 2017 at 17:57 Matt asked Jun 19, 2017 at 17:46 MattMatt 3951 gold badge6 silver badges15 bronze badges 4
  • 3 Your code correctly tests contacts[i].hasOwnProperty(prop) but then attempts to return contacts.prop. It should be contacts[i][prop]. – Pointy Commented Jun 19, 2017 at 17:48
  • 3 You return too early in that outer else case. You can only state to have found no such contact after having iterated all of them. – Bergi Commented Jun 19, 2017 at 17:49
  • @Pointy I modified it to return contacts[i].prop; but I am still getting the same error. I also tried the bracket notation version which you provided. – Matt Commented Jun 19, 2017 at 17:50
  • @Matt there's a big difference between contacts[i][prop] and contacts[i].prop – Pointy Commented Jun 19, 2017 at 18:11
Add a ment  | 

1 Answer 1

Reset to default 5

You have to return the value of the variable as key

return contact[i][prop]; 

By doing contacts[i].prop you are returning the attribute named prop: contact[i]['prop']

Complet Function

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].hasOwnProperty(prop))
          return contacts[i][prop]; 
        else 
          return "No such property"; 
      }
      else
        return "No such contact"; 
    }

// Only change code above this line
}

Also you're returning for every case something the first iteration.. Even if the name is later on the array. Let's try something

function lookUpProfile(firstName, prop) {   
    // Try to find the right contact
    for(var i = 0; i<contacts.length; i++) {
      // Name is found
      if(contacts[i].firstName == firstName){ 
        // Return prop if exist, or error message
        if(contacts[i].hasOwnProperty(prop))
          return contacts[i][prop]; 
        else 
          return "No such property"; 
      } 
    }
    // No one with this name is found, return error
    return "No such contact";
}

My way to do it (not tested)

function lookUpProfile(firstName, prop) {   
    let contact = null
    // Try to find the right contact
    contacts.forEach(c => {
      if (!contact && c && c.firstName && c.firstName == firstname) {
        contact = c
      }
    })

    if (!contact)
      return "No such contact";
    return contact.hasOwnProperty(prop) ? contact[prop] : "No such property"
} 
发布评论

评论列表(0)

  1. 暂无评论