How are object keys referenced, rather than the value of the key?

Tell us what’s happening:
I’ve been running this through pythontutor constantly with little adjustments and have figured out that I don’t know how to reference a key name rather than the value of the key in an object. For instance: In this exercise you have to see if prop is equal to a key in one of the objects in the array, to do that, I’m thinking if (contacts[i].prop === prop) should work. I’ve tried many other variations like contacts.i - contacts[i] - contacts[prop] etc. but no matter what I see the code will break at that point in the loop since it doesn’t return true any more.

In short: How do I reference the name of a key in an object that’s part of an array?

Also, I am aware that my if… else loop at the bottom doesn’t work properly, that’s something I’ll worry about later.

Your code so far


//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(name, prop){
// Start
for (var i = 0; i < contacts.length; i++) {
        if (contacts[i]["firstName"] === name) {
            if (contacts[i].prop === prop) {
                return contacts[i][prop];
            }
        }
}

if (name !== contacts["firstName"]) {
    return "No such contact";
} else if (prop !== contacts[i]) {
    return "No such property"
}

// Stop
}

// Change these values to test your function
lookUpProfile("Kristian", "lastName");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

Between all the combinations you are missing just the correct one

You need to access the element at index i in an array so contacts[i] is correct
That is an object, and to access a property with the property name value in a variable
you need to use bracket notation
contacts[i][prop]

       if (contacts[i].prop === prop) {

You don’t want this, check what contacts[i][prop] is and what prop is, that can never be true

Try searching a way to check if an object has a property, google is your friend

1 Like

I’ve been googling and I can’t seem to word it right, that’s my biggest issue right now. No matter what I search I can only find how to look for the value of a key rather than the key itself.

However, you just reminded me that I can simply make sure that prop isn’t blank… So now I’m passing every single point except for one that I will have no problem figuring out!!! Thank you!!!

I have searched “javascript does object have property”

Found this, they explain well why just checking if a property isn’t blank may not always work (I have seen one of the challenges where a property had value of null, in this case it wouldn’t work)

I am here, so let’s post also the link to the documentation:

1 Like

Going to check this reply as the solution instead since that did also clear up an issue I ran into with the last point, I only ran into the issue because I was trying to check if the prop wasn’t blank.

So it turns out .hasOwnProperty is going to be used quite a lot, I just can’t seem to remember learning about it through FCC up to this point and it makes me think I shouldn’t have to use it.

You learn about the Read-Search-Ask method quite early, there is no reason for which you shouldn’t use it

FreeCodeCamp doesn’t pretend to teach you JavaScript, it shows you some of the main concepts and some of the most used methods and operators - but these are challenges, not lessons.

Makes more sense, so really it should be used in conjunction with other tools that may touch base more on these concepts

So when you arrive at the projects, you can do it with just what freecodecamp showed you, or you can go to learn new things!

Also, freecodecamp barely scrap the surface on many methods, if you want to learn new things about what you already know, check the methods you are using in the documentation (MDN is recommended)