1.0.6 • Published 6 years ago

nested-get v1.0.6

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

nested-get

Get nested value inside a string, an array or an object.

stability-unstable

npm version Known Vulnerabilities dependency status devdependency status build status Code Climate downloads

NPM

Compatibility

  • Server : Node.js >= 6.0.0
  • Browser :
    • Chrome >= 49
    • Firefox >= 34
    • Edge >= 12
    • Opera >= 37

Use

get(data, filter)

  • data (string||object): Contains the datas to get.
  • filter (number||object||string): Defines the content to get from data. See the examples below.

String Operators on objects

If the filter argument is a string, it can contain the following operators in order to get nested object attributes.

  • a.b : Get the b attribute of the object a.
  • a.* : Get everything from the array a. Acts like a map() function.
  • a.*.b : Map on the array a with the b attribute.
  • ~a : Keep the key/value data of the object a instead of only its value. This operator can only be placed before an object attribute.
    • e.g. : on { a: 1, b: 2 } => gets { a: 1 } instead of just 1.

Examples

const get = require('nested-get');

// Get from a string
get('foo', 1);
// => 'o'
get('foo', [0, 1]);
// => ['f', 'o']

// Get from an object
get({ a: 1, b: 1 }, 0);
// => { a: 1 }
get({ a: 1, b: 1 }, 'a');
// => 1
get({ a: 1, b: { c: 2 } }, 'b.c');
// => 2
get({ a: 1, b: { c: 2 } }, 'b.0');
// => { c: 2 }
get({ a: [1, 2, 3] }, 'a.1');
// => 2
get({ a: 1, b: 1 }, ['a', 0]);
// => [1, { a: 1 }]

// Get from an array
get([1, 2, 3], 1);
// => 2
get([1, 2, 3], [0, 1]);
// => [1, 2]

// '*' acts like a '.map()' function
get([{ a: 1, b: 2 }, { a: 3, b: 4 }], '*.a');
// => [1, 3]
get([{ a: 1, b: 2 }, { a: 3, b: 4 }], '*.0');
// => [{ a: 1 }, { a: 3 }]
get([{ a: 1, b: 2 }, { a: 3, b: 4 }], 'a'); // Works like '*.a' if applied to an array
// => [1, 3]

// '~' => Keeps the full object :
get([{ a: 1, b: 2 }, { a: 3, b: 4 }], '*.~a');
// => [{ a: 1 }, { a: 3 }]

Alternatives

NPM

License

MIT License

Copyright (c) 2018 Nicolas COUTIN

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.