add after ary assign assignIn assignInWith assignWith at before bind bindAll bindKey camelCase capitalize castArray ceil chain chunk clamp clone cloneDeep cloneDeepWith cloneWith compact concat conformsTo countBy create curry curryRight debounce deburr defaults defaultsDeep defer delay difference divide drop dropRight each eachRight endsWith entries entriesIn eq escape every extend extendWith fill filter find findIndex findKey findLast findLastIndex findLastKey first flatMap flatMapDeep flatMapDepth flatten flattenDeep flattenDepth flip floor forEach forEachRight forIn forInRight forOwn forOwnRight fromPairs functions functionsIn get groupBy gt gte has hasIn head inRange includes indexOf initial intersection invert invertBy invoke invokeMap isArguments isArray isArrayBuffer isArrayLike isArrayLikeObject isBoolean isBuffer isDate isElement isEmpty isEqual isEqualWith isError isFinite isFunction isInteger isLength isMap isMatch isMatchWith isNaN isNative isNil isNull isNumber isObject isObjectLike isPlainObject isRegExp isSafeInteger isSet isString isSymbol isTypedArray isUndefined isWeakMap isWeakSet join kebabCase keyBy keys keysIn last lastIndexOf lowerCase lowerFirst lt lte map mapKeys mapValues max maxBy mean meanBy memoize merge mergeWith min minBy multiply negate nth omit omitBy once orderBy overArgs pad padEnd padStart parseInt partial partialRight partition pick pickBy pull pullAll pullAllBy pullAllWith pullAt random rearg reduce reduceRight reject remove repeat replace rest result reverse round sample sampleSize set setWith shuffle size slice snakeCase some sortBy sortedIndex sortedIndexOf sortedLastIndex sortedLastIndexOf sortedUniq split spread startCase startsWith subtract sum sumBy tail take takeRight tap template throttle thru toArray toFinite toInteger toLength toLower toNumber toPairs toPairsIn toPlainObject toSafeInteger toString toUpper transform trim trimEnd trimStart truncate unary unescape union uniq uniqBy uniqWith unset unzip unzipWith update updateWith upperCase upperFirst values valuesIn without words wrap wrapperAt wrapperChain wrapperCommit wrapperFlatMap wrapperLodash wrapperNext wrapperPlant wrapperReverse wrapperToIterator wrapperValue xor zip zipObject zipObjectDeep zipWith

Lodash _.hasIn

Overview

Lodash is a popular JavaScript library that provides utility functions for common programming tasks. One of the useful methods in Lodash is the _.hasIn() method, which allows you to check whether an object has a nested property.

The _.hasIn() method takes two parameters: the object to check and the path of the nested property to evaluate. The method returns a boolean value indicating whether the property exists or not.

Here’s the basic syntax for using _.hasIn():

_.hasIn(object, path)

The _.hasIn() method uses dot notation to specify the path of the nested property. For example, if you have an object user with a nested property address.street, you can use the following code to check if the street property exists:

_.hasIn(user, 'address.street')

If the street property exists, the method will return true. Otherwise, it will return false.

You can also use bracket notation to specify the path of the nested property. For example:

_.hasIn(user, ['address', 'street'])

This is equivalent to using dot notation with a string path. The bracket notation may be useful if the path contains special characters or spaces.

The _.hasIn() method is useful for checking if a property exists in deeply nested objects. For example, let’s say you have a complex object with many nested properties, and you want to check if a specific property exists:

const obj = {
  a: {
    b: {
      c: {
        d: 'hello world'
      }
    }
  }
}

_.hasIn(obj, 'a.b.c.d'); // true
_.hasIn(obj, 'a.c.b'); // false

In this example, the _.hasIn() method correctly returns true for the d property but false for the b property in the a.c path.

It is important to note that _.hasIn() method also checks for properties that are inherited from the object’s prototype chain. If you only want to check for properties that are directly defined on the object, you can use the hasOwnProperty() method instead.

In summary, the _.hasIn() method is a useful utility function for checking if nested properties exist in JavaScript objects. It supports both dot and bracket notation for specifying property paths, and it can be used to check for inherited properties as well.