path-to-prop

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

Usage no npm install needed!

<script type="module">
  import pathToProp from 'https://cdn.skypack.dev/path-to-prop';
</script>

README

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'

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

getProp(target, path)
  // returns 1

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

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

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

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