Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Get the value of a nested object key (including array indexes) if it exists, or undefined if it doesn't, without worrying about checking you still have a value at each level.
Kinda like
isSet
from that other language, but you get the value back too.
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';
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!
FAQs
Get the value of a nested object key (including array indexes) if it exists, or undefined if it doesn't, without worrying about checking you still have a value at each level.
The npm package getpath receives a total of 0 weekly downloads. As such, getpath popularity was classified as not popular.
We found that getpath demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.