Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-nested-value

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-nested-value

Utility package to get deeply nested values from objects. Support arrays.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
decreased by-63.16%
Maintainers
1
Weekly downloads
 
Created
Source

/**

  • @fileoverview
  • This package safely returns the value at a requested path inside of
  • an object. Best used on unpredictable deeply nested structures. Supports
  • arrays as well.
  • Install:
  • npm install get-nested-value --save
  • or

  • yarn add get-nested-value
  • Example:
  • import getNestedValue from 'get-nestedValue';
  • getNestedValue('some.path.to.key', object);
  • // or
  • getNestedValue(['some', 'path', 'to', 'key'], object);
  • // dealing with arrays?
  • getNestedValue(['some', 'path', 'atIndex', 0, 'key'], object);
  • Notes:
    • Returns undefined if path does not exist.
    • Throws an Error if path is not a string or array. */

/**

  • @param {array|string} path – e.g ['key1', 0, 'key2'] or a string such as 'key1.0.key2
  • @param {object} object
  • @returns {*} */ const getNestedValue = (path, object) => { if (!Array.isArray(path) && typeof path !== 'string') { throw Error('The package get-nested-value received a non-contract path. Please provide a string or an array!'); } return (Array.isArray(path) ? path : path.split('.')) .reduce((prev, cur) => (prev && prev[cur] !== undefined ? prev[cur] : undefined), object); };

module.exports = getNestedValue;

Keywords

FAQs

Package last updated on 12 Jun 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc