Profile Lookup - ID and access to OBJ's

Profile Lookup - ID and access to OBJ's
0.0 0

#1

Hi, I just had a question regarding the structure of OOP in Javascript. I’ve worked with OOP in other contexts before, but I find myself getting a little tripped up as to how things are laid out here. Namely, I believe I understand the ‘hasOwnProperty’ qualifer-- Or I believe it to be searching over the entire domain, in this case for any ‘firstName’ matches and thus returns true/false accordingly.

But what I am having a harder time understanding is then how does one ‘pivot’, or effectively do a combined search for the other properties of that object that shares that identifier.

I.e. the way they are defined here appears to be all on the same hierarchical level, with no ‘master identifier’ for that particular object. One could of course, sort of ‘assume’ that if ‘firstName’ exists, then simply do the same for the qualifying property… But this seems to get tricky fast. I mean what if you had duplicate firstName’s ?

I guess what I am asking is under this schema, how does one: 1) Uniquely identify any one object. 2) Based upon any one given property, gain access to the objects other properties at the same hierarchical level (i.e. given a found ‘first name’, access a last name ? In other cases you might be able to create a pointer to that object, perhaps, based on one of its properties… But I don’t feel like I am seeing it yet here).

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

// Change these values to test your function
lookUpProfile("Akira", "likes");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:


#2

contacts is an array and even though in JavaScript, arrays are objects, it is a special case. Arrays have indices to identify them where objects have properties. Arrays have elements referenced by indices.

For example:

var myArray = ['Spring','Summer','Fall','Winter'];
myArray[1]; // reference to 'Summer'

var myObject = {
   seasons:  ['Spring','Summer','Fall','Winter'];
}
myObject.seasons[1]; // reference to 'Summer'

The hasOwnProperty function only applies to objects and not arrays.