This package has been deprecated

Author message:

Just use lodash.get

getpath

0.1.1 • Public • Published

getPath

Kinda like isSet from that other language, but you get the value back too.

What

getPath takes an object (or array), and a string representing the path to a key you want to find within it. If it exists you'll get the value back, and if it doesn't you'll just get undefined. When you can't guarantee a value exists within the object this saves you from testing at every level.

In other words,

let myValue = 'default';

if (myObject && myObject.maybe && myObject.maybe[1] && myObject.maybe[1].maybeNot) {
	myValue = myObject.maybe[1].maybeNot;
}

becomes

const myValue = getPath(myObject, 'maybe[1].maybeNot') || 'default';

How

getPath takes two arguments - the object you want to find something within, and the path to what you want as a string. The path can include dot-notation and array indices.

If you have an object called a and want to use the value of a.b[1].c if it exists, just call getPath(a, 'b[1].c'). If that exists, you'll get it, and if any part of that path doesn't exist you'll just get undefined. Easy!

Readme

Keywords

none

Package Sidebar

Install

npm i getpath

Weekly Downloads

5

Version

0.1.1

License

WTFPL

Last publish

Collaborators

  • callummr