Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash get vs. es6 fallback values?

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?

like image 864
abrarisme Avatar asked Aug 22 '17 07:08

abrarisme


1 Answers

The advantage of _.get is, you omit continuing checks if a property exist, which would be necessary.

latitude: response && response.pickupLocation && response.pickupLocation.latitude || "",
like image 68
Nina Scholz Avatar answered Oct 01 '22 18:10

Nina Scholz