Recall the following code? You need to retrieve a property value deep in an object. It smells, doesn’t it?
How can we write it better?
1 | const extractFirstErrorDetails = (errorResponse) => { |
using lodash
_.get(errorResponse, 'data.errors[0].detail', '')
this is the lodash get method
_.get(object, path, [defaultValue])
Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.