Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Check if nested object has property, the easy way.
Antiquated way:
var exists = Boolean(
obj &&
obj.qux &&
obj.qux.zee &&
obj.qux.zee.peep &&
obj.qux.zee.peep[2] &&
obj.qux.zee.peep[2].__data);
with hasProp:
var exists = hasProp(obj, ['qux', 'zee', 'peep', 2, '__data']);
npm install hasprop
var hasProp = require('hasprop');
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)
hasProp(obj, ['foo']) // true
hasProp(obj, ['deedee']) // false
hasProp(obj, ['qux', 'zee', 'boop']) // true
hasProp(obj, ['qux', 'zee', 'peep', 0]) // true
hasProp(obj, ['qux', 'zee', 'peep', 1]) // true
hasProp(obj, ['qux', 'key.with.dots']) // true
hasProp(obj, ['qux', '"key.with.quotes"', 'greet']) // true
hasProp(obj, ['qux', 'zee', 'peep', 2]) // true
hasProp(obj, ['qux', 'zee', 'peep', 2, '__data']) // true
hasProp(obj, ['qux', '$el']) // true
hasProp(obj, ['foo.bar']) // true
hasProp(obj, ['qux', 'qux']) // false
// string for path
hasProp(obj, 'foo') // true
hasProp(obj, 'deedee') // false
hasProp(obj, 'qux.zee.boop') // true
hasProp(obj, 'qux.zee.peep.0') // true
hasProp(obj, 'qux.zee.peep.1') // true
hasProp(obj, 'qux.zee.peep[1]') // true
hasProp(obj, 'qux[key.with.dots]') // true
hasProp(obj, 'qux["key.with.quotes"].greet') // true
hasProp(obj, 'qux.zee.peep.2') // true
hasProp(obj, 'qux.zee.peep.2.__data') // true
hasProp(obj, 'qux.$el') // true
hasProp(obj, '[foo.bar]') // true
hasProp(obj, 'qux.qux') // false
Partially applied:
var objHasProp = hasProp(obj);
objHasProp(['foo']) // true
objHasProp('[foo.bar']) // true
objHasProp(['qux']) // true
objHasProp(['yo']) // false
For getting the value, check out the module getprop.
MIT
[0.0.4] - 2016-01-05
FAQs
Check if nested object has property
The npm package hasprop receives a total of 6 weekly downloads. As such, hasprop popularity was classified as not popular.
We found that hasprop 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.