# object-bystring

> Access and alter objects using string literals

Latest version **8.0.1** (published 2025-11-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-bystring
pnpm add object-bystring
yarn add object-bystring
bun add object-bystring
```

## Health

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

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 8.0.1 |
| Published | 2025-11-12 |
| First published | 2017-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | tamb |
| Maintainers | tamb |
| Keywords | object, bystring, string, utility, mutable |

## Links

- npm: https://www.npmjs.com/package/object-bystring
- Repository: https://github.com/tamb/object-bystring
- Homepage: https://github.com/tamb/object-bystring#readme
- Issues: https://github.com/tamb/object-bystring/issues
- npm.io page: https://npm.io/package/object-bystring

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 8.0.1 (latest) — 2025-11-12
- 8.0.0 (alpha) — 2025-09-05
- 7.0.3 — 2025-08-22
- 7.0.2 — 2025-08-22
- 7.0.1 — 2025-08-22
- 7.0.0 — 2025-08-22
- 6.0.2 — 2024-02-17
- 6.0.1 — 2024-02-16
- 6.0.0 — 2024-02-15
- 6.0.0-alpha.1 — 2024-02-15
- 5.0.3 — 2023-02-20
- 5.0.2 — 2023-02-18
- 5.0.1 — 2020-06-01
- 5.0.0 — 2020-05-30
- 4.2.0 — 2020-05-30
- … 14 more at https://npm.io/package/object-bystring/versions

## README

# object-bystring

Access and alter deeply nested object properties by string notation

## Purpose

Let's say you want to change a something nested in this object:

```js
var objA = {
  name: "tamb",
  address: {
    street: "123 fake street",
    town: "fake town",
    zip: "00000",
    state: "Fake State",
    city: "Fake City",
    POBoxes: [1234, 23213, 2321],
  },
  dependents: [
    {
      name: "John Doe",
      age: 55,
    },
    {
      name: "Jane Doe",
      age: 44,
    },
  ],
};
```

You coould easily write `objA.dependents[1].age = 45;`  
 Or even `var indx = 1; objA.dependents[indx].age = 45;`

No issue here.

But let's say you are trying to pass along an object (`objB`) of commands. Those commands should dictate what to change in `objA`.

How do you easily do this?

```js
{
 'address.street': '345 Faker Way',
 'dependents[1].age': 45,
 'address.POBoxes[2]': 43278
}
```

With `byString` you can generate paths to object values and either set or get those values.

## Latest Docs:

https://github.com/tamb/object-bystring

## Demo

https://codesandbox.io/embed/object-bystring-demo-i3845d?fontsize=14&hidenavigation=1&theme=dark

## installation

`npm install --save object-bystring`

## Usage

You can import either a utility method, or a polyfill to add this functionality to the `Object` prototype.

```js
// util method
const { bystring } = require("object-bystring");
import { byString } from "object-bystring";
```

Using the example above:

### Setting Values

#### `byString(object, key, value)`

```js
byString(objectA, "path.to.field", "new value");
```

setting values for fields that don't exist will add them to the object.
Setting values for array indexes that don't exist will add them to the array and other indexes will be undefined.

````js

### Getting Values

#### `byString(object, key);`

```js
const finger = byString(person, "arm[0].hand.fingers[3]");

const randomFinger = byString(person`arm[0].hand.fingers[${number}]`);
````

Getting values for fields that don't exist will return `undefined`.

### Performance & Optimization History

The `byString` function has undergone significant optimizations across multiple versions:

#### **V8 (Current) - Ultra-Optimized**

V8 delivers **79.55% performance improvement** over the original version (5,000,000 iterations):

| Version        | Time         | vs Original       | vs V7             |
| -------------- | ------------ | ----------------- | ----------------- |
| Original       | 1,034.04ms   | -                 | -                 |
| V7             | 543.41ms     | **47.46% faster** | -                 |
| **V8 (Ultra)** | **211.45ms** | **79.55% faster** | **61.09% faster** |

**V8 Key Optimizations:**

- Single-pass, zero-allocation inline algorithm
- Eliminated helper functions and intermediate objects
- Direct array/object creation and navigation
- Optimized peek-ahead logic for type determination
- Minimal string slicing operations

#### **V7 Optimizations**

V7 introduced modular architecture with:

- Character-by-character parser (replaced regex)
- Separated concerns (`parseKey`, `setValue`, `getValue`)
- Enhanced TypeScript support and type safety
- Better error handling and edge case coverage

#### **Breaking Changes in V8**

⚠️ **Important**: V8 introduces breaking changes for edge cases:

**1. Malformed Array Syntax**

```js
// Before (Original/V7): Creates nested property
byString(obj, "array[", "value");
// Result: obj.array["["] = "value"

// After (V8): Creates literal property name
byString(obj, "array[", "value");
// Result: obj["array["] = "value"
```

**2. Non-numeric Array Indices**

```js
// Before (Original/V7): Creates nested property
byString(obj, "array[abc]", "value");
// Result: obj.array["[abc]"] = "value"

// After (V8): Creates literal property name
byString(obj, "array[abc]", "value");
// Result: obj["array[abc]"] = "value"
```

**Impact**: These changes affect only malformed or non-standard syntax. Valid array notation (`array[0]`, `array[123]`) and object notation (`obj.prop`) work identically.

**Recommendation**: Use proper syntax (`array[0]` for arrays, `obj.prop` for objects) to avoid these edge cases.

### Attribution

Thank you, Ray for the original Stackoverflow answer, which is the inspiration for this project.
https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key

Check out his github and so profile below:

https://github.com/raybellis

https://stackoverflow.com/users/6782/alnitak

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