Lodash 与使用新的 ES6 可选参数相比如何?
我有以下代码:
location: {
latitude: response.pickupLocation.latitude || "",
longitude: response.pickupLocation.longitude || ""
},
有了 Lodash,我知道我可以运行:
latitude: get(response, 'pickupLocation.latitude', '')
或者我可以创建一个接受对象和路径并始终返回 ''
作为默认回退的函数。除了代码会更短之外,在这里使用 Lodash 还有什么优势吗?
最佳答案
_.get
的优势也就是说,您省略了持续检查属性是否存在,这是必要的。
latitude: response && response.pickupLocation && response.pickupLocation.latitude || "",
关于javascript - Lodash get vs. es6 后备值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811667/