After having a look at this example from the Lodash-documentation of the has-method:
var object = { 'a': { 'b': 2 } };
_.has(object, 'a.b');
// => true
I asked myself: What's the actual purpose of using this method?
Wouldn't be ...
if (object.a.b) {
...
}
the same and isn't more code too?
In vanilla JS, your code will throw an error if object, or object.a is undefined:
const object = {};
if (object.a.b) {
}
Thus the lodash method.
console.log(_.has({}, 'a.b'));
console.log(_.has(undefined, 'a.b'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With