How to check that javascript object contains the given key? – User friendly Tech help

A wet man does not fear the rain.

n

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

n

Example:-

n

// Sample Objectnconst REMINDER = "REMIND_ME";nreminderObj= {nINBOX:`${REMINDER}_INBOX`,nUSERS:`${REMINDER}_USERS`,nGROUPS:`${REMINDER}_GROUPS`,n}

n

Object.prototype.hasOwnProperty.call(reminderObj,"INBOX");

nNote:- We could directly use hasOwnProperty but to avoid eslint errors we have used the longer syntax as above.n

const reminderLocation = "INBOX";nnreminderObj[reminderLocation];nnornreminderObj.INBOX;n
Was this article helpful?
YesNo

Similar Posts