dash-get
A tiny get function, similar to Lodash.get
Last updated 6 years ago by itsjonq .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install dash-get 
SYNC missed versions from official npm registry.

✊ (dash) get

Build Status npm version

A tiny get function', similar to Lodash.get

✨ Features

  • Zero dependencies!
  • Super tiny, at ~200 bytes gzipped
  • Works almost exactly like Lodash.get
  • Ultra speedy! Check out the performance tests

🔧 Installation

Add dash-get to your project via npm install:

npm install --save dash-get

🕹 Usage

You can easily retrieve a value from a (deeply) nested object with dash-get, like so:

import get from 'dash-get'

const someObject = {...}

const deeplyNestedValue = get(someObject, 'the.path.to.the.nested.value')
// value

The path could also be an Array:

const someObject = {...}

const deeplyNestedValue = get(someObject, ['the', 'path', 'to', 'the', 'nested', 'value'])
// value

🎬 API

get(obj, path, fallback)

Argument Type Description
obj Object The object to get the value from.
path Array<string>/string The path to the value.
fallback any The fallback value, in case the desired value could not be retrieved.

👻 Unsupported feature

This module does not support this particular use case:

get(object, 'a[0].b.c')

🤔 Why an npm module tho?

You totally don't have to npm install this. This exists for convenience purposes 😊.

In fact, it's encouraged that you add the get code to your code base! One less depenency to install and manage.

Here it is!

function get(obj, path, fallback) {
  if (!obj || !path) return fallback;
  const paths = Array.isArray(path) ? path : path.split(".");
  let results = obj;
  let i = 0;

  while (i < paths.length && results !== undefined && results !== null) {
    results = results[paths[i]];
    i++;
  }

  if (i === paths.length) {
    return results !== undefined ? results : fallback;
  }

  return results !== undefined && results !== null ? results : fallback;
}

❤️ Thanks

Thanks to @knicklabs for pairing with me on this one!

Current Tags

  • 1.0.2                                ...           latest (6 years ago)

3 Versions

  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (5)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE