how to use the hasownproperty function from lodash in javascript

You can use the hasOwnProperty function from lodash to check if an object has a specific property. Here's an example:

index.tsx
// import the hasOwnProperty function from lodash
const has = require('lodash/hasOwnProperty');

// create an object
const myObj = {name: 'John', age: 30};

// check if the object has a property called 'name'
if (has(myObj, 'name')) {
  console.log('myObj has a property called name');
} else {
  console.log('myObj does not have a property called name');
}

// check if the object has a property called 'address'
if (has(myObj, 'address')) {
  console.log('myObj has a property called address');
} else {
  console.log('myObj does not have a property called address');
}
572 chars
20 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