@bnidev/js-utils
    Preparing search index...

    Function getNestedValue

    • Retrieves a nested value from an object using a dot-separated path. If the value is undefined or null, it returns the provided fallback value.

      Type Parameters

      • T
      • R = unknown

      Parameters

      • obj: T

        The object to retrieve the value from.

      • path: string

        The dot-separated path to the nested value.

      • Optionalfallback: R

        The value to return if the nested value is undefined or null.

      Returns unknown

      The nested value or the fallback value if not found.

      This utility is useful for safely accessing deeply nested properties in objects without having to check each level for existence. It can help avoid runtime errors when trying to access properties that may not exist.

      // ES Module
      import { getNestedValue } from '@bnidev/js-utils'

      // CommonJS
      const { getNestedValue } = require('@bnidev/js-utils')
      const data = {
      user: {
      profile: {
      name: 'John Doe',
      age: 30,
      address: null
      }
      }
      }

      // With fallback provided:
      const name = getNestedValue(data, 'user.profile.name', 'Unknown')
      console.log(name) // Output: 'John Doe'

      const address = getNestedValue(data, 'user.profile.address', 'No address provided')
      console.log(address) // Output: 'No address provided' (because address is null)

      const phone = getNestedValue(data, 'user.profile.phone', 'No phone number')
      console.log(phone) // Output: 'No phone number' (property doesn't exist)

      // Without fallback:
      const age = getNestedValue(data, 'user.profile.age')
      console.log(age) // Output: 30

      const email = getNestedValue(data, 'user.profile.email')
      console.log(email)