📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

nested-obj

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nested-obj

manipulate deep objects safely and simply

0.0.1
latest
Source
npm
Version published
Maintainers
1
Created
Source

nested-obj

nested-obj is a simple and lightweight JavaScript utility library for safely accessing and modifying nested properties in objects using string paths.

Features

  • Get: Safely access nested properties in an object.
  • Set: Set a value at a specific path in an object. Does not overwrite if the value is undefined.
  • Has: Check if a specific path exists within an object.

Installation

npm install nested-obj

Usage

Get

Retrieve a nested property value from an object.

import objectPath from 'nested-obj';

const obj = {
  user: {
    name: 'John Doe',
    address: {
      street: '123 Main St',
      city: 'Anytown'
    }
  }
};

const userName = objectPath.get(obj, 'user.name');
console.log(userName); // 'John Doe'

Set

Set a value at a specific path in an object. If any part of the path does not exist, it will be created.

objectPath.set(obj, 'user.address.zip', '12345');
console.log(obj.user.address.zip); // '12345'

Has

Check if a path exists within an object.

const hasCity = objectPath.has(obj, 'user.address.city');
console.log(hasCity); // true

Running Tests

To run tests, execute the following command:

npm test

or for continuous

npm test:watch

FAQs

Package last updated on 31 Mar 2024

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