npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

object-bystring

v6.0.2

Published

Access and alter objects using string literals

Downloads

32

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:

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?

{
 '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.

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

Using the example above:

Setting Values

byString(object, key, value)

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.


### 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.

Attribution

Thank you, Ray for the original Stackoverflow answer, which is the inspiration for the source code. 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