Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
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.
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'
lodash.get is a method from the Lodash library that provides similar functionality to get-value. It allows for retrieving nested values with a default option. Lodash is a larger utility library, so lodash.get is part of a broader suite of tools.
dot-prop is another package that allows for getting and setting nested properties. Unlike get-value, dot-prop also supports setting values. It uses a dot notation string as the path.
deep-get-set is a package that not only gets but also sets deep values. It is less popular than get-value and does not have as many configuration options.
Use property paths (
a.b.c
) to get a nested value from an object.
Install with npm:
$ npm install get-value --save
Install with bower
$ bower install get-value --save
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'
// works with arrays
get(obj, 'e.0.f');
//=> 'g'
Escape dots
var obj = {'foo/bar.md': {b: 'c'}};
get(obj, 'foo/bar\\.md');
//=> {b: c}
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'
You might also be interested in these projects:
key
exists deeply on the given object. | homepage'a.b.c'
) paths. | homepagePull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v, on March 27, 2016.
FAQs
Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).
The npm package get-value receives a total of 5,257,005 weekly downloads. As such, get-value popularity was classified as popular.
We found that get-value demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.