[Javascript] Safer property access with Lodash's 'get' method
Property access in Javascript can be problematic - especially when dealing with nested Objects and Arrays. Doing it manually and in a safe manner requires tons of boilerplate inside conditionals and results in a defensive style of coding. In this lesson we look at why this is a problem & how to overcome it using the get method from the popular utility library Lodash
var data = {
"response": {
"body": {
"deviceDetail": {
"deviceDetails": [
{
"handsetIMEI": 7356383,
"handsetDateLastUsed": "2019-04-20T01:02:03.812Z",
},
{
"handsetIMEI": 34534,
"handsetDateLastUsed": "2019-04-20T01:02:03.812Z",
}
]
}
}
}}
const ns = ['response', 'body', 'deviceDetail', 'deviceDetails'];
const handsetIMEI = _.get(data, ns.concat([0, 'handsetIMEI']))
console.log(handsetIMEI)
[Javascript] Safer property access with Lodash's 'get' method的更多相关文章
- [Javascript] Intercept property access with Javascript Proxy
A Javascript Proxy object is a very interesting es6 feature, that allows you to determine behaviors ...
- 6.2.3 Property Access Errors
JavaScript: The Definitive Guide, Sixth Edition by David Flanagan Property access expressions do n ...
- JavaScript 中 Property 和 Attribute 的区别详解
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- 消除警告"property access result unused - getters should not be used for side effects"
我写了如下一段代码: - (void)btnClicked:(UIButton *)button { switch (button.tag) { : self.initShare; break; de ...
- 使用asp.net MVC的 HtmlHelper 时遇到的小问题,报错:Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
异常信息:Templates can be used only with field access, property access, single-dimension array index, or ...
- [Javascript Crocks] Safely Access Nested Object Properties with `propPath`
In this lesson, we’ll look at the propPath utility function. We’ll ask for a property multiple level ...
- [Javascript Crocks] Safely Access Object Properties with `prop`
In this lesson, we’ll use a Maybe to safely operate on properties of an object that could be undefin ...
- php中直接执行mysqli_init()也是报Property access is not allowed yet的错误。
xdebug.auto_trace = On 和 xdebug.profiler_enable = On注释掉就OK了,不知道这两个配置项是干嘛的
- javascript array.property.slice.call
function foo() { //var var1=Array.prototype.slice.call(arguments); var var1=[].slice.call(arguments) ...
随机推荐
- Android图像处理之熔铸特效
代码: package com.color; import android.content.Context; import android.graphics.Bitmap; import androi ...
- OpenCV —— 轮廓
把检测出的边缘像素组装成轮廓 —— cvFindContours OpenCV 使用内存存储器来统一管理各种动态对象的内存.内存存储器在底层被实现为一个有许多相同大小的内存块组成的双向链表 内存储 ...
- passwd---设置用户密码的相关信息
passwd命令 passwd命令用于设置用户的认证信息,包括用户密码.密码过期时间等.系统管理者则能用它管理系统用户的密码.只有管理者可以指定用户名称,一般用户只能变更自己的密码. 语法 pas ...
- 洛谷 P2867 [USACO06NOV]大广场Big Square
P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...
- (九)unity4.6学习Ugui中文文档-------參考-UGUI Rect Transform
大家好.我是孙广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unit ...
- eclipse个人插件
1.SVN eclipse markets 安装m2e-subversion.svnkit 2.maven 本地装好mvn prefences导入maven安装目录和配置 3.单元测试覆盖率 EclE ...
- 动态库dll使用module.def文件导出函数(像静态库一样使用)
1.新建文件module.def. 2.动态库工程上右键->属性->链接器->输入->模块定义文件编辑它写入module.def 3.下面为module.def实例(smart ...
- JS / CSS 实现的便签记录本
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- vue中类名和组件经过刷新不对应的解决办法
方法一: 页面路由如下: index.js路由文件如下: { path: '/myOrder', name: '我的订单', menuShow: true, component: myOrder, c ...
- CF-833B The Bakery(线段树优化Dp)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredie ...