
Security News
CISA Kills Off RSS Feeds for KEVs and Cyber Alerts
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Get a property from nested object, the easy way.
Antiquated way:
var value = 'default value';
if (
obj &&
obj.qux &&
obj.qux.zee &&
obj.qux.zee.peep &&
obj.qux.zee.peep[2] &&
obj.qux.zee.peep[2].__data) {
value = obj.qux.zee.peep[2].__data;
}
with getProp:
var value = getProp(obj, ['qux', 'zee', 'peep', 2, '__data']);
npm install get-prop
bower install getprop
var getProp = require('get-prop');
var obj = {
foo: 'bar',
qux: {
zee: {
boop: 4,
peep: [55,'zonk', {
__data: 'pow'
}],
},
'key.with.dots': 'hello',
'"key.with.quotes"': {
greet: 'hi'
},
$el: 'element'
},
'foo.bar': 'noob',
qax: null
};
// array for path (recommended)
getProp(obj, ['foo']) // 'bar'
getProp(obj, ['deedee']) // undefined
getProp(obj, ['deedee'], "I'm default value") // "I'm default value"
getProp(obj, ['qux', 'zee', 'boop']) // 'yo'
getProp(obj, ['qux', 'zee', 'peep', 0]) // 55
getProp(obj, ['qux', 'zee', 'peep', 1]) // 'zonk'
getProp(obj, ['qux', 'key.with.dots']) // 'hello'
getProp(obj, ['qux', '"key.with.quotes"', 'greet']) // 'hi'
getProp(obj, ['qux', 'zee', 'peep', 2]) // {__data: 'pow'}
getProp(obj, ['qux', 'zee', 'peep', 2, '__data']) // 'pow'
getProp(obj, ['qux', '$el']) // 'element'
getProp(obj, ['foo.bar']) // 'noob'
getProp(obj, ['qux', 'qux']) // undefined
// string for path
getProp(obj, 'foo') // 'bar'
getProp(obj, 'deedee') // undefined
getProp(obj, 'deedee', "I'm default value") // "I'm default value"
getProp(obj, 'qux.zee.boop') // 'yo'
getProp(obj, 'qux.zee.peep.0') // 55
getProp(obj, 'qux.zee.peep.1') // 'zonk'
getProp(obj, 'qux.zee.peep[1]') // 'zonk'
getProp(obj, 'qux[key.with.dots]') // 'hello'
getProp(obj, 'qux["key.with.quotes"].greet') // 'hi'
getProp(obj, 'qux.zee.peep.2') // {__data: 'pow'}
getProp(obj, 'qux.zee.peep.2.__data') // 'pow'
getProp(obj, 'qux.$el') // 'element'
getProp(obj, '[foo.bar]') // 'noob'
getProp(obj, 'qux.qux') // undefined
Partially applied:
var objProp = getProp(obj);
objProp(['foo']) // 'bar'
objProp('[foo.bar']) // 'noob'
objProp(['qux']) // 'noob'
objProp(['yo'], 'wut') // 'wut'
For a boolean version of this, check out the module hasprop.
MIT
FAQs
Get a property from object
The npm package get-prop receives a total of 37,818 weekly downloads. As such, get-prop popularity was classified as popular.
We found that get-prop 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
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.