DEV Community

Cover image for 4 Ways To Check if an Object Key Exists
Jamie Mc Manus
Jamie Mc Manus

Posted on

4 Ways To Check if an Object Key Exists

1. hasOwnProperty

This will return a boolean value depending on whether the object on which you are calling it has a property with given name as an argument e.g. obj.hasOwnProperty('keyname')

Full Example:
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}

class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}

let eng = new Engineer("John","Doe","BSC")

console.group("hasOwnProperty");
console.log(eng.hasOwnProperty('firstName'))      // Output: true
console.log(eng.hasOwnProperty('qualification'))  // Output: true
console.log(eng.hasOwnProperty('nonexistantkey')) // Output: false
console.groupEnd();

Enter fullscreen mode Exit fullscreen mode

2. Object.keys

Object.keys will return an array of all the keys belonging to the given object.

You can then check the array to see if a particular key exists. It is the longest of all the methods.

class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}

class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}

let eng = new Engineer("John","Doe","BSC")

console.group("iteration");
for (const key of Object.keys(eng)) {
   console.log(key)
   //Perform Other Check ..
}

/* Results:
"firstName"
"lastName"
"qualification"
*/

console.groupEnd();

// Or

Object.keys(eng).filter(x => x.includes("keyname"));

Enter fullscreen mode Exit fullscreen mode

3. in operator

The in operator returns true if the property is in the object

class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}

class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}
let eng = new Engineer("John","Doe","BSC")

console.group("in operator");
console.log('firstName' in eng)      // Output: true
console.log('qualification' in eng)  // Output: true
console.log('nonexistantkey' in eng) // Output: false
console.groupEnd();

Enter fullscreen mode Exit fullscreen mode

4. Reflect.has()

Reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.

Therefore using the Reflect api you can check and/or manipulate an Objects properties, methods and variables.

You can use Reflect.has(targetObject, propertyKey) method to check if the key exists.

Full Example
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}

class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}

let eng = new Engineer("John","Doe","BSC")
console.group("Reflect.has");
console.log(Reflect.has(eng,'firstName'))       // Output: true
console.log(Reflect.has(eng,'qualification'))   // Output: true
console.log(Reflect.has(eng,'nonexistantkey'))  // Output: false
console.groupEnd();
Enter fullscreen mode Exit fullscreen mode

Slán go fóill

Well that's it, each is simple enough and you can choose whichever to do the task.

Feel free to ask questions, comment or contribute below!

And if you're feeling generous you can buy me a coffee with the link below ( and yes its all for coffee, I drink a copious amount of it while writing ☕ )

Buy Me A Coffee

Discussion (0)