Socket
Socket
Sign inDemoInstall

get-value

Package Overview
Dependencies
0
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    get-value

Use property paths (`a.b.c`) to get a nested value from an object.


Version published
Weekly downloads
12M
decreased by-8.73%
Maintainers
1
Install size
6.35 kB
Created
Weekly downloads
 

Package description

What is get-value?

The get-value npm package is used for safely retrieving nested values from an object or array. It is useful when dealing with deeply nested structures where checking for the existence of each level can be cumbersome. It allows for specifying paths to the desired value using a string or an array of keys/indices.

What are get-value's main functionalities?

Get nested values

Retrieve a nested value from an object using a string path.

const get = require('get-value');
const obj = { a: { b: { c: 'd' } } };
console.log(get(obj, 'a.b.c')); // 'd'

Use array paths

Retrieve a nested value using an array of keys as the path.

const get = require('get-value');
const obj = { a: { b: { c: 'd' } } };
console.log(get(obj, ['a', 'b', 'c'])); // 'd'

Specify default values

Provide a default value to return if the full path does not exist.

const get = require('get-value');
const obj = { a: { b: { c: 'd' } } };
console.log(get(obj, 'a.b.e', { default: 'default value' })); // 'default value'

Split string paths

Retrieve values from keys that include a dot or other special characters by specifying a custom separator.

const get = require('get-value');
const obj = { 'a.b': { c: 'd' } };
console.log(get(obj, 'a\.b.c', { separator: '\.' })); // 'd'

Other packages similar to get-value

Readme

Source

get-value NPM version Build Status

Use property paths (a.b.c) to get a nested value from an object.

Install

Install with npm

$ npm i get-value --save

Install with bower

$ bower install get-value --save

Usage

var get = require('get-value');

var obj = {a: {b : {c: {d: 'foo'}}}, e: [{f: 'g'}]};
get(obj, 'a.b.c');
//=> {d: 'foo'}

get(obj, 'a.b.c.d');
//=> 'foo'

get(obj, 'e[0].f');
//=> 'g'

key as an array

Optionally pass the key as an array (this is useful when you need to dynamically build up the property name)

var obj = {a: {b: 'c'}};
get(obj, ['a', 'b']);
//=> 'c'
  • has-any: Returns true if an object has any of the specified keys. | homepage
  • has-any-deep: Return true if key exists deeply on the given object. | homepage
  • has-value: Returns true if a value exists, false if empty. Works with deeply nested values using… more | homepage
  • set-value: Create nested values and any intermediaries using dot notation ('a.b.c') paths. | homepage
  • unset-value: Delete nested properties from an object using dot notation. | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running tests

Install dev dependencies:

$ npm i -d && npm test

Author

Jon Schlinkert

License

Copyright © 2014-2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on October 29, 2015.

Keywords

FAQs

Last updated on 29 Oct 2015

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc