A wet man does not fear the rain.

Issue:-How do I check if an object has a key in JavaScript?

Example:-

// Sample Object
const REMINDER = "REMIND_ME";
reminderObj= {
INBOX:`${REMINDER}_INBOX`,
USERS:`${REMINDER}_USERS`,
GROUPS:`${REMINDER}_GROUPS`,
}

Problem1: Now we want to check that our object have “INBOX” as key or not?

Solution:- 

Object.prototype.hasOwnProperty.call(reminderObj,"INBOX");
Note:- We could directly use hasOwnProperty but to avoid eslint errors we have used the longer syntax as above.

Problem2: Now we want to check what is the value for given key say “INBOX” inside our object?

const reminderLocation = "INBOX";

reminderObj[reminderLocation];

or
reminderObj.INBOX;

 javascript object

Leave a Reply

Your email address will not be published. Required fields are marked *