how to use the hasin function from lodash in javascript

The hasIn function from Lodash allows you to check if a given property key exists in a nested object by passing in the object and the property path as arguments. Here's an example of how to use it:

index.tsx
const _ = require('lodash');

const myObject = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St.',
    city: 'Anytown',
    state: 'CA'
  }
};

console.log(_.hasIn(myObject, 'name')); // true
console.log(_.hasIn(myObject, 'address.city')); // true
console.log(_.hasIn(myObject, 'address.zipCode')); // false
326 chars
16 lines

In this example, we require the lodash library and create an object called myObject with some nested properties. We then use the hasIn function to check if the name property exists (which it does), if the address.city property exists (which it does), and if the address.zipCode property exists (which it does not).

By using the hasIn function, we can avoid getting errors when trying to access properties that don't exist within a nested object.

gistlibby LogSnag