[Javascript] Safer property access with Lodash's 'get' method

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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的主要内容,如果未能解决你的问题,请参考以下文章

初步了解Windows Safer API

safe的比较级 如题,是safer还是more safe 这两者都可以? 完全懂的讲解一下

英语munge symlinks to make them safer怎么翻译?

关于ipywidgets插件,错误'Widget Javascript not detected. It may not be installed or enabled properly.&

如何在Javascript中制作自定义对象? [复制]

案例使用PHP的内置函数,通过DES算法对数据加密和解密