🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

path-to-prop

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

path-to-prop

Retrieves a property from an object based on a 'path/to.that.prop'

latest
Source
npmnpm
Version
3.0.2
Version published
Weekly downloads
2.4K
1.6%
Maintainers
1
Weekly downloads
 
Created
Source

Path to prop 🛤

Total Downloads Latest Stable Version

npm i path-to-prop

Retrieves a property from an object based on a 'path/to.that.prop'

  • Supports paths with both / and . to separate props
  • Safely typed (with TypeScript) - returns unknown

Usage

import { getProp } from 'path-to-prop'

getProp({ nested: { prop: 1 } }, 'nested.prop') // 1

getProp({ nested: { prop: 1 } }, 'nested') // { prop: 1 }

You can use / or . to access nested props:

const obj = {a: {b: {c: {d: {e: 1}}}}}
const path = 'a/b/c.d.e'

getProp(obj, path)
  // returns 1

getProp(obj, 'nonexistent.prop')
  // returns undefined

When you have / or . in your prop names, use an array:

const obj = {'a/b': {'c.d': 1}}

getProp(obj, ['a/b', 'c.d'])
  // returns 1

Keywords

path-to-prop

FAQs

Package last updated on 19 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts