Lodash / library / has - Passed test, but doesn't do what it is supposed to?

Hi people,

I would really appreciate help here since I am really confused right now.

The code below in the function block passes the test in lodash, but only if I write object[key] and throws an error with object.key
Returns false if an object does not have a value at a specified key - Failed: _.has({“key”: “value”}, “notKey”) returned true instead of false
WHY?

Also, I tried to test the code myself in a short example, but it doesn’t work now matter which I use and the boolean test behaves really weird. Here I can test with obj.name but not obj[name].

const obj = { name: "Paul", age: null, eyes: undefined } function has (object, key) { if (object[key] !== undefined) { return true; } else { return false; } } console.log(has(obj, name)); // ReferenceError: name is not defined --> but code passes test in lodash console.log(obj.name !== undefined); // returns true console.log(obj[name] !== undefined); // ReferenceError: name is not defined

Thanks,
Paul

Because there is no property, key, only ‘name’, ‘age’ and ‘eyes’. key is a variable that temporarily refers to each. Only properties the object has can be accessed with dot notation syntax.

In my understanding key is the parameter of the function so if I want to check if the name property has a value I would use the following code → _.has(obj, name) which would then be passed into the function as → if (object.key !== undefined)

Maybe I wasn’t fully clear in my description. The example above is me trying to use it in an example case.

My code from the lodash project is the following :

const _ = { has (object, key) { if (object.key !== undefined) { return true; } else { return false; } } };

The questions remain the same:
Why can I not use object[key] and object.key not interchangeably?
and
Why does the example throw an error?

It is not possible to access object.key unless the object has that actual property. A parameter is a variable, so must be accessed as a subscript of the property.

 object[key]

One other thing, though, a property can have a value, undefined which means i wouldn’t be found in that object.

object.hasOwnProperty(key)

is more definitive, and looks for the actual property indicated by the key variable.

1 Like

So whenever I am working with objects in functions, I will always work with “Bracket-notation”, because writing object.key will look for the “key”-property in my object which in this case doesn’t exist.

Ist that correct?

That is correct. When iterating over an object with an iteration variable, we must assume there is no such property in the object so always use bracket notation. In the event the property actual does exist, it won’t have any effect and will work as expected.

Still, keep in mind that undefined is a legitimate value to assign to a key. It simply means no defined value has yet been assigned, not that the does not exist.

obj {
    prop: undefined
}

obj.prop === undefined    //  true

And yet the key does exist.

1 Like