You can use the hasOwnProperty
function from lodash to check if an object has a specific property. Here's an example:
index.tsx572 chars20 lines
In this example, we import the hasOwnProperty
function from lodash using require
. We then create an object called myObj
with two properties, name
and age
. We use the has
function to check if myObj
has a property called 'name'
. If it does, we log a message saying so. If not, we log a message saying that myObj
does not have a property called 'name'
. We then do the same thing to check for the 'address'
property.
Note that we use the has
function instead of the normal hasOwnProperty
method because it is more convenient and also handles nested properties.
gistlibby LogSnag