dpgt

1.0.0 • Public • Published

deepget

Small package to get deep nested properties from JavaScript objects

Install

# npm 
npm install dpgt --save
 
# yarn 
yarn add dpgt

Use

Import the utility and get nested properties passing the object and the key path.

import deepget from 'dpgt';
 
// some object with nested properties
const cartoon = {
  title: 'Rick and Morty',
  seasons: 3,
  characters: {
    rick: {
      firstName: 'Rick'
      lastName: 'Sanchez'
    },
    morty: {
      firstName: 'Morty'
      lastName: 'Smith'
    },
  }
};
 
deepget(cartoon, 'characters.rick.lastName');
// Sanchez

You can also use an array as a key path.

deepget(cartoon, [ 'characters', 'rick', 'lastName' ]);
// Sanchez

Default values are supported too as a thrid optional argument.

deepget(cartoon, [ 'characters', 'beth' ], 'Not found');
// Not found

TODO

  • Write tests
  • Write contributing guide

Package Sidebar

Install

npm i dpgt

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

7.5 kB

Total Files

10

Last publish

Collaborators

  • jeremenichelli