[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

时间: 2024-11-09 23:58:17

[Javascript] Safer property access with Lodash's 'get' method的相关文章

6.2.3 Property Access Errors

JavaScript: The Definitive Guide, Sixth Edition by David Flanagan Property access expressions do not always return or set a value. This section explains the things that can go wrong when you query or set a property. It is not an error to query a prop

JavaScript 中 Property 和 Attribute 的区别详解

property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property是DOM中的属性,是JavaScript里的对象: attribute是HTML标签上的特性,它的值只能够是字符串: 基于JavaScript分析property 和 attribute html中有这样一段代码: <input id="in_1" value="1&quo

JavaScript基础 使用js给表单增加method属性

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"

Caused by: org.hibernate.property.access.spi.PropertyAccessBuildingException: Could not locate getter for property named [pers.chq.entity.Customer#custLevel]

Could not locate getter for property named [pers.chq.entity.Customer#custLevel] 属性名字不一致的错误, 原因:在hibernate的配置文件中所配置的属性和实体类中的属性名不一致. 原文地址:https://www.cnblogs.com/weichenchq/p/8232671.html

javascript array.property.slice.call

function foo() { //var var1=Array.prototype.slice.call(arguments); var var1=[].slice.call(arguments); console.log(var1.length); } foo(0,1,2,3,4,5,6);

24个JavaScript初学者最佳实践

这里面说到的一个就是使用循环新建一个字符串时,用到了join(),这个比较高效,常常会随着push(); 绑定某个动作时,可以把要执行的绑定内容定义为一个函数,然后再执行.这样做的好处有很多.第一是可以多次执行,第二是方便调试,第三是方便重构. As a follow-up to “30 HTML and CSS Best Practices”, this week, we’ll review JavaScript! Once you’ve reviewed the list, be sure

1.1 Core JavaScript

This section is a tour of JavaScript language, and also a tour of Part I of this book. After this introductory chapter, we dive into JavaScript at the lowest level: Chapter 2, Lexical Structure, explains things like JavaScript comments, semicolons, a

Property 和 Attribute 的区别(转)

property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property是DOM中的属性,是JavaScript里的对象: attribute是HTML标签上的特性,它的值只能够是字符串: 基于JavaScript分析property 和 attribute html中有这样一段代码: <input id="in_1" value="1&quo

JavaScript: Class.method vs Class.prototype.method

在stack overflow中看到一个人回答,如下   // constructor function function MyClass () { var privateVariable; // private member only available within the constructor fn this.privilegedMethod = function () { // it can access private members //.. }; } // A 'static m