Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
/ js-get Public archive

Retrieve deeply nested properties from mixed objects and arrays using dot notation.

License

Notifications You must be signed in to change notification settings

hypefactors/js-get

JS-Get

A simple and lightweight dot-notation based property resolver.

Build Status GitHub release npm

Contents

Why JS-Get

  • Fast
  • Lightweight, well-documented source code bundled as CJS, EM and UMD
  • 100% test coverage
  • Supports objects and arrays

Installation

To install JS-Get, just run the following:

$ npm install @hypefactors/js-get

Note: This package was only tested on Node.js versions 9 and 8 or higher.

Usage

Import the module via import get from '@hypefactors/js-get' or if you're not using a bundler use require instead (Node environment).

Given that there is an object with the following structure:

const profile = {
  age: 15,
  name: {
    first: 'John'
  },
  activities: [
    'sports',
    {
      name: 'fishing',
      frequency: 'weekly'
    }
  ]
}

The properties can be accessed using dot notation:

get(profile, 'age') // 15
get(profile, 'name.first') // 'John'
get(profile, 'activities.0') // 'sports'
get(profile, 'activities.1.frequency') // 'weekly'
get(profile, 'foobar', 'not-found') // 'not-found'

Or using array syntax:

get(profile, ['age']) // 15
get(profile, ['name', 'first']) // 'John'
get(profile, ['activities', '0']) // 'sports'
get(profile, ['activities', '1', 'frequency']) // 'weekly'
get(profile, ['foobar'], 'not-found') // 'not-found'

Contributing

Thanks for your interest in JS-Get! If you'd like to contribute, please read our contribution guide.

License

JS-Get is open-sourced software licensed under the ISC license. If you'd like to read the license agreement, click here.

About

Retrieve deeply nested properties from mixed objects and arrays using dot notation.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published