How does Lodash compare to using the new ES6 optional arguments?
I have the following code:
location: {
latitude: response.pickupLocation.latitude || "",
longitude: response.pickupLocation.longitude || ""
},
With Lodash I know I could run:
latitude: get(response, 'pickupLocation.latitude', '')
Or alternatively I could create a function that takes in the object and path and always returns ''
as the default fallback. Is there any advantage to using Lodash here other than the fact that the code would be shorter?
The advantage of _.get
is, you omit continuing checks if a property exist, which would be necessary.
latitude: response && response.pickupLocation && response.pickupLocation.latitude || "",
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