[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) ...
随机推荐
- 带你彻彻底底弄懂Scroller
Scroller的使用 这是一个滑动帮助类.并不能够使View真正的滑动,而是依据时间的流逝.获取插值器中的数据.传递给我们.让我们去配合scrollTo/scrollBy去让view产生缓慢滑动,产 ...
- 深入分析JavaWeb Item23 -- jsp自己定义标签开发入门
一.自己定义标签的作用 自己定义标签主要用于移除Jsp页面中的java代码. 二.自己定义标签开发和使用 2.1.自己定义标签开发步骤 1.编写一个实现Tag接口的Java类(标签处理器类) 要编写一 ...
- jquery12 queue() : 队列方法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- C#运算符小例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- vue 点击事件阻止冒泡 用stop
1.使用vue阻止子级元素的click事件冒泡,很简单,用stop.eg: @click.stop='xxx'
- vsphere client和vsphere web client的区别
vsphere client是一个运行在windows桌面上的客户端,在linux环境下无法运行,在vsphere5.0以后,VMware在逐渐弱化vsphere client的作用,现在很多高级功能 ...
- C# WinForm设置透明
1:通过设置窗体的 TransparencyKey实现 例:窗体中的白色会变成透明 this.BackColor =Color.White; this.TransparencyKey = ...
- Chrome不能在网易网盘中上传文件的解决办法
Chrome不能在网易网盘中上传文件的解决办法1. 安装 Adobe Flash Player PPAPI,设置flash插件 chrome://settings/content/flash,许可[* ...
- 对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法。提供下载
对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法.提供下载 对该PE 中的 XP 系统作了汉化, 由于一个 中文字库 就要 10M 多:加之原系统过于精简,对中文 ...
- 用Navicat连接MySQL数据库出现1251错误:密码方式错误
原因:因为MySQL8.0是最新版密码保存方式,而图形化数据库管理工具还是原先的密码保存方式. 解决方式: 用CMD命令号方式进入MySQL use mysql: ALTER USER 'root'@ ...