Lodash project, problem at the has() method

I have a problem with implementing of the has() method:

const _ = {

    has (obj, key) {

        if (obj.key !== undefined) {
          return true;
        } else {
          return false;
        }

      }
};

const object = {
    key : 'value'
};

So if I check the object is all ok:

console.log(object.key);          // value
console.log(object.notKey);   // undefined 

But if I try to use the has() method I always get “true”:

console.log(_.has(object, 'key'));         // true
console.log(_.has(object, 'notKey'));  // true

What do I wrong?

here:

if (obj.key !== undefined)

you check if object has property key. It does. You don’t use the parameter of the has method here. To access a key of an object with a string, you have to use associative array notation.

2 Likes

It worked, thanks for a quick response!

1 Like