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. JAVA 并发编程-线程范围内共享变量(五)

    线程范围内共享变量要实现的效果为: 多个对象间共享同一线程内的变量 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsi ...

  2. SQL 增删改查(具体)

    一.增:有3种方法 1.使用insert插入单行数据: insert [into] <表名> [列名] values <列值> insert into Strdents (na ...

  3. h5播放音乐

    h5音频播放,里面參数能够查看http://www.w3school.com.cn/html5/html_5_audio.asp <audio controls="controls&q ...

  4. SQL查询表中的用那些索引

    方法1. 使用系统表   -- 查询一个表中的索引及索引列 USE AdventureWorks2008 GO SELECT indexname = a.name , tablename = c. n ...

  5. Impala储存与分区

    不多说,直接上干货! hive的元数据存储在/user/hadoop/warehouse Impala的内部表也在/user/hadoop/warehouse. 那两者怎么区分,看前面的第一列. 下面 ...

  6. DG 参数详解

    1.与角色无关的参数 ◆ DB_UNIQUE_NAME:数据库唯一名.对于物理standby,DB_NAME必须相同,对于逻辑standby,DB_NAME可以不同,所以在10g中引入DB_UNIQU ...

  7. Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory(转)

    1. Spring整合MyBatis切换SqlSessionFactory有两种方法,第一. 继承SqlSessionDaoSupport,重写获取SqlSessionFactory的方法.第二.继承 ...

  8. Spring项目用junit 时出现org.junit.runners.BlockJUnit4ClassRunner cannot be resolved(转)

    spring框架项目用junit做测试时,程序在自动编译时出现下述问题: 程序的问题是项目中找不到org.junit.runners.BlockJUnit4ClassRunner,有两种可能,一是没有 ...

  9. C# 向DataTable中插入数据或伪造DataTable

    方法一: DataTable  tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Col ...

  10. 对ng-repeat的表格内容添加不同样式:ng-style

    对ng-repeat的表格内容添加不同样式,html代码: <tr ng-repeat="x in tableData"> <td>{{x.networkN ...