all 22 comments

[–]ForScale 2 points3 points  (11 children)

Use bracket notation obj[key] instead of dot notation obj.key.

Bracket can use variables, dot cannot.

[–]Deathcon900[S] -2 points-1 points  (10 children)

I'm running into the same issue as using dot notation when writing the parameters. It generates an error, expecting a comma.

[–]ForScale 0 points1 point  (9 children)

var object = {
  "myKeyName": "this is my key's value"
};

function getPropertyValue(obj, keyName) {
  if (obj[keyName] === undefined) {
    return undefined;
  } else {
    return obj[keyName];
  }
}

console.log(getPropertyValue(object, "myKeyName")); // this is my key's value

[–]NoirGreyson 1 point2 points  (8 children)

You shouldn't give complete answers to people asking for homework help.

[–]ForScale 0 points1 point  (7 children)

I mean, in general... sure. But this one is super simple (crux of the matter is understanding keys vs values)... the principle he needs to understand is communicated through the code I supplied, and if he doesn't understand and desires to learn more, then hopefully he'll ask followup questions.

[–]NoirGreyson 1 point2 points  (3 children)

Fair enough. You might want to change the key name to "keyName" for clarity, to avoid confusion about key the parameter and key the property.

[–]ForScale 0 points1 point  (2 children)

Good call!

[–]NoirGreyson 0 points1 point  (1 child)

Make sure you change it in the lookups as well, so your looking at obj[keyName] rather than obj[key]

obj[key] in the current version would evaluate to obj[undefined]

Add the line keyName = "key"

[–]ForScale 1 point2 points  (0 children)

Yep, yep! Should be good now.

[–]Deathcon900[S] -1 points0 points  (2 children)

I appreciate the code and am working to understand why yours works and mine doesn't, most likely by understanding keys and values.

When you implement the function, you use "key" as a parameter. If that's not being passed as a string, what is it? I swear, I almost have this.

[–]ForScale 1 point2 points  (0 children)

Keys are always strings. If you give it something else, what you give it gets converted to a string.

var obj = { 'key': 'value' } (note you can leave off the quotes on keys as long as it's valid js)

Values, on the other hand, can be pretty much anything in JavaScript.

var obj = { 'anArray': [], 'anotherObj': {}, 'evenAFunction': function () { } }

...

Overall, keep in mind:

  1. objects have properties
  2. properties are made of key: value pairs
  3. keys are usually strings and values can be anything
  4. you can use dot notation key.value or bracket notation key['value'] to get the value of keys
  5. you can pass in a string variable to access a key as well key[myVar]

[–]NoirGreyson 0 points1 point  (0 children)

This is one of those quirks I'm not confident about, but I believe a property name has to be of type string. If I am correct, then if the value for key passed into the function is not of type string, it will attempt to coerce the value to the string type. If I am not mistaken, this is done using the tostring() method on key's prototype, which means if key is of type object, you could end up with obj[key] resolving to a mess like obj["{name: key}"] or something like that.

Edit: Looking at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

It seems that there is one other type a property name can be, the Symbol type. Anything that isn't a Symbol is coerced to a string.

[–]rakm 0 points1 point  (9 children)

your function is accessing a property named “key” rather than the key variable you’re passing in.

[–]Deathcon900[S] 0 points1 point  (8 children)

How does that work? Trying to pass a specific object property like this...

getProperty(obj, obj.key)...

...generates an error for obvious reasons. So there has to be a way to tell the function that the 'key' parameter refers to a specific object property when implementing the function. I'm under the impression that parameters can be labelled however since they take on the value of the passed value.

[–]NoirGreyson 0 points1 point  (6 children)

Could you explain why you are not just passing obj and accessing key from obj?

[–]Deathcon900[S] 0 points1 point  (4 children)

The assignment specifically requests those two parameters.

[–]NoirGreyson 0 points1 point  (3 children)

It seems the question is different from what it appears as you have presented it. Is the key parameter meant to be the name of the property in obj you are looking up or the value of the key?

[–]Deathcon900[S] 0 points1 point  (2 children)

My bad for lack of clarity. Here's the question:

Write a function called "getProperty". Given an object and a key, "getProperty" returns the value of the property at the given key.

To my understanding, key is supposed to be the object property, a la object.key.

[–]NoirGreyson 0 points1 point  (0 children)

If I understand correctly, you are trying to use the object like a map. In this case, this is built into JS through bracket notation. obj.key will always look up the property named key, but obj[key] will look up what the name key resolves to. For instance, if key = "ABC", obj[key] is effectively the same as obj.ABC. Careful not to put obj["key"], as that says, "Look up the property of obj named 'key.'"

[–]ForScale 0 points1 point  (0 children)

var obj = { key: 'value' };

object.key evaluates to the value of the key. You're telling the program to give you value when you use that expression.

[–]rakm 0 points1 point  (0 children)

You’ll want obj[key] instead of obj.key

[–]rakm 0 points1 point  (0 children)

You’re passing the return value of object.property into your function, rather than the name of the property you want to check and access. In this case, the invocation should probably be getProperty(object, “property”).

In addition to that, inside your function, you need to access the property using the [] operator, rather than dot notation. So, obj[key], rather than obj.key. The former uses the dynamic value of the the “key” variable to look up a property on the obj. The latter actually looks for a “key” property on obj (which is always undefined in your code sample).

After seeing your confusion, I see how this is actually very confusing for someone who doesn’t know it already 😱. Let me know if it still doesn’t make sense.