testing object for properties
Hi all!
Do not understand, why in line 4 to adress the value “pony” of the only matching object in the list - out of 3 - I have to use obj[checkProp]. If I only use “return obj” I get the whole list of the 3 objects. Why is obj[checkProp] not returning all properties of all 3 objects? The filtering is not clear to me. And what if I just wanted to display the property of the matching object gift: “pony” ? Can I adress only its property “gift” somehow? "return obj[property?] " . I can not work with indexes here to address the property.
Thank you so much!
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)){
return obj[checkProp]; // <- do not understand this
}
else{
return "Not Found";
}
// Only change code above this line
}
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift");
Hm I do not get it. All 3 value-pairs are passed to the function at the same time as arguments? Or one after the other and processed like an iteration?
The function is only called 1x. This is confusing.
In run time the object is passed to the function checkObj, parameter, obj. The property in question is passed to the function, checkObj, parameter, checkProp. So if obj.hasOwnProperty (checkProp) is true obj[checkProp] will return the associated value with the property in question of the object that was passed into the parameter, obj.
So in line 4 that is calling ‘pony’ from the value passed into value associated with the property “gift”. It might be confusing to look at because gift is passed as a string but that is for the computer to recognize it as a data type that can be compared with the properties in the object. As someone mentioned before. They are key value pairs, such that gift is the key and “pony” is the corresponding value.
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)){
return obj[checkProp];
}
else{
return “Not Found”;
}
Hey everyone
Can someone please help me understand why we had to use obj[checkProp] instead of just return checkProp furthermore why can’t we use dot notation like this "obj.checkProp?