Is there any way to write this more elegantly?
if ("a" in obj && "b" in obj.a && "c" in obj.a.b)
instead of checking for existence on each stage of the object?
If I do if ("c" in obj.ab)
this will give error if a
or b
doesn't exist.
I've always found the lodash _.has and _.get functions very powerful, you can access deeply nested values in objects without a lot of code to check the path exists.
For example:
const obj = { a: { b: { c: 42 }}}; if (_.has(obj, "abc")) { console.log("Path exists"); } // Safe to access, will return undefined if path does not exist... console.log("Path value:", _.get(obj, "abc"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
function pluckDeep(key) {
return function(obj) {
return key.split('.').reduce((accum, key) => {
return accum[key]
}, obj);
}
}
const obj = { a: { b: { c: 42 }}};
if (pluckDeep('a.b.c')(obj)) {
// do something
}
This curried function can determine if a property exist by using the first parameter of the pluckDeep function.
if (obj.a && obj.a.b && obj.a.b.c) {
// do something
}
if (obj.a?.b?.c) {
// do something
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.