# object-property-extractor

> A lightweight (no dependencies) tool to extract deeply nested values from JS Objects (or Arrays), with optional Fallback.

Latest version **1.0.14** (published 2026-02-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-property-extractor
pnpm add object-property-extractor
yarn add object-property-extractor
bun add object-property-extractor
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.14 |
| Published | 2026-02-10 |
| First published | 2022-05-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Carl Smith |
| Maintainers | carlosnz |

## Links

- npm: https://www.npmjs.com/package/object-property-extractor
- Repository: https://github.com/CarlosNZ/object-property-extractor
- npm.io page: https://npm.io/package/object-property-extractor

## Recent versions

- 1.0.14 (latest) — 2026-02-10
- 1.0.13 — 2024-12-16
- 1.0.12 — 2024-08-23
- 1.0.11 — 2024-05-11
- 1.0.10 — 2024-02-13
- 1.0.9 — 2024-02-13
- 1.0.8 — 2024-02-03
- 1.0.7 — 2023-08-06
- 1.0.6 — 2023-01-06
- 1.0.5 — 2023-01-06
- 1.0.4 — 2023-01-06
- 1.0.3 — 2022-09-04
- 1.0.2 — 2022-05-31
- 1.0.1 — 2022-05-31
- 1.0.0 — 2022-05-20

## README

# Object Property Extractor

Access deep object properties using a string (e.g. "user.country.name")

A lightweight (no dependencies) tool to extract deeply nested values from JS Objects (or Arrays), with optional Fallback.

Similar to [Lodash' `get`](https://lodash.com/docs/4.17.15#get) function, but with some additional functionality.

## Why?

Consider the object
```js
const data = {
  user: {
    name: { first: 'Jango', last: 'Fett' },
    children: ['Boba', 'Clone 1', 'Clone 2', ...etc],
    weapons: [
      { name: 'Blaster', description: 'For shooting stuff' },
      { name: 'Seismic charge', description: '...BWAAAAAANG' },
    ],
  },
  ...otherProperties,
}
```

In Javascript, you call inner object properties via dot notation:
```js
data.user.name.last // Fett
```

If you want to access a property dynamically, you can do this:
```js
const key = "user" 
return data[key]
```

However, you *can't* do this:
```js
const key = "user.name"
return data[key]
```

This tool allows access to deep properties from a single "property path" string.

## Installation

```js
yarn add object-property-extractor
// OR
npm install object-property-extractor
```

## Usage

`extract( dataObject, propertyString, [fallback] )`

```js
import extract from "object-property-extractor"

// Using the data object above
extract(data, "user.name.first") // Jango

// With fallback when path not found
extract(data, "user.age", "Unknown") // Unknown

// Arrays can be accessed by index, as per normal indexing syntax
extract(data, "user.children[1]") // Boba
```

### Array handling

In addition to accessing array by index (above), if an array consists of objects, then it's possible to extract a single property from *each* object in the returned array.

For example:
```js
extract(data, "user.weapons.name")
// ["Blaster", "Seismic charge"]
```

Note that this is essentially a shorthand for:  
`extract(data, "user.weapons").map((weapon) => weapon.name)`

### Error handling

If a requested property can't be accessed (e.g. incorrect path), the function will *throw* an error, unless a fallback is provided. So unless you are catching and handling these errors at a higher level, it is recommended to always provided a fallback (`null` is an acceptable fallback).

## Testing

A [jest](https://jestjs.io/) test suite is included in the repo. To run:
`yarn test`

See `/test/test.ts` for the test cases.

## Bug report / Feature requests

Please make an issue in the Github repo: https://github.com/CarlosNZ/object-property-extractor

---
_Source: https://npm.io/package/object-property-extractor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
