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的更多相关文章

  1. [Javascript] Intercept property access with Javascript Proxy

    A Javascript Proxy object is a very interesting es6 feature, that allows you to determine behaviors ...

  2. 6.2.3 Property Access Errors

    JavaScript: The Definitive Guide, Sixth Edition by David Flanagan   Property access expressions do n ...

  3. JavaScript 中 Property 和 Attribute 的区别详解

    property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...

  4. 消除警告"property access result unused - getters should not be used for side effects"

    我写了如下一段代码: - (void)btnClicked:(UIButton *)button { switch (button.tag) { : self.initShare; break; de ...

  5. 使用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 ...

  6. [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 ...

  7. [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 ...

  8. php中直接执行mysqli_init()也是报Property access is not allowed yet的错误。

    xdebug.auto_trace = On 和 xdebug.profiler_enable = On注释掉就OK了,不知道这两个配置项是干嘛的

  9. javascript array.property.slice.call

    function foo() { //var var1=Array.prototype.slice.call(arguments); var var1=[].slice.call(arguments) ...

随机推荐

  1. [React] Write Compound Components

    Compound component gives more rendering control to the user. The functionality of the component stay ...

  2. 浏览器加载渲染HTML、DOM、CSS、 JAVASCRIPT、IMAGE、FLASH、IFRAME、SRC属性等资源的顺序总结

    页面响应加载的顺序:   1.域名解析->加载html->加载js和css->加载图片等其他信息 DOM详细的步骤如下: 解析HTML结构. 加载外部脚本和样式表文件. 解析并执行脚 ...

  3. BZOJ5027: 数学题

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 140  Solved: 48[Submit][Status][Discuss] Description ...

  4. 虚拟机中试用windows 8(视频)

    虚拟机中试用windows 8(视频) VM7装windows 8基本没戏,建议用正式版vmware8.0,还有Oracle的Virtualbox 也没问题http://www.virtualbox. ...

  5. js中的组合模式

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. hadoop常用指令

    1.格式化名称节点:hadoop namenode -format 2.操作hadoop文件系统:hadoop fs -ls / 3.设置ubuntu默认以命令行方式启动 sudo gedit /et ...

  7. 用Ngen指令加快C#程序的启动速度

    用Ngen指令加快C#程序的启动速度 由于C#是使用实时 (JIT) 编译器编译原始程序集.因此第一次运行C#程序(或Dll)时,程序的启动非常慢.为了提高用户的体验,可以用Microsoft的供的本 ...

  8. CodeForcesGym 100502H Clock Pictures

    Clock Pictures Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGy ...

  9. Python - 字典(dict)删除元素

    字典(dict)删除元素, 能够选择两种方式, dict.pop(key)和del dict[key]. 代码 # -*- coding: utf-8 -*- def remove_key(d, ke ...

  10. [NowCoder]牛客OI周赛3

    A.地斗主 题意:\(4\times N\) 的地板,在上面铺 \(1\times 2\) 和 \(2\times 1\) 的地砖,求铺满方案数, \(N\le 10^9\) 原题..先把一列的状态压 ...