博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
三层嵌套对象数组怎么解构_嵌套解构
阅读量:2519 次
发布时间:2019-05-11

本文共 1570 字,大约阅读时间需要 5 分钟。

三层嵌套对象数组怎么解构

Destructuring in JavaScript can initially feel confusing but the truth is that destructuring can make your code a bit more logical and straight forward. Destructuring does look a bit more complex when you're looking for a property several objects deep, so let's have a look at how to do that!

使用JavaScript进行结构分解最初可能会感到困惑,但事实是,结构分解可以使您的代码更具逻辑性和直截了当。 当您要寻找一个深几个对象的属性时,解构的过程看起来会更加复杂,所以让我们来看看如何做到这一点!

Simple destructuring looks as follows:

简单的解构如下所示:

const { target } = event;

Here we use {} and = to name a variable the same as the property name (you can also use an ). Grabbing a nested object value is a bit more complicated, however:

在这里,我们使用{}=来命名与属性名称相同的变量( 也可以使用 )。 但是,获取嵌套对象的值要复杂一些:

// Object for testingconst x = { y: { z: { a: 1, b: 2} } }// Get "b"const { y: { z: { b } } } = x;console.log(b); // 2console.log(z); // z is not definedconsole.log(y); // y is not defined

Here we an object-like syntax with {} and : to set a var based on the nested obect property. Note that only the last nested property is given as a variable; the parents we reference along the way do not.

在这里,我们使用{}:的类对象语法来基于嵌套的obect属性设置var。 注意,只有最后一个嵌套属性作为变量给出; 我们沿途推荐的父母没有。

To get a reference to both b and y, for example, you can use a comma:

例如,要同时引用by ,可以使用逗号:

const { y, y: { z: { b } } } = x;console.log(b); // 2console.log(y); // {z: {…}}

Destructuring can take a while to get used to but, the more I use it, the more I appreciate how simple my code can be: no "dot" hell and less overall code!

解构可能需要一段时间才能习惯,但是,我使用它的次数越多,我就越欣赏我的代码多么简单:没有“点”地狱,总的代码也更少!

翻译自:

三层嵌套对象数组怎么解构

转载地址:http://skvwd.baihongyu.com/

你可能感兴趣的文章
nodejs + express 热更新
查看>>
ClientScriptManager.RegisterClientScriptBlock Method 无效
查看>>
asp.net web site中reference的version的autoupdate
查看>>
第4章 网络层
查看>>
volatile
查看>>
项目需求分析答辩总结
查看>>
mysql-6正则表达式
查看>>
廖雪峰Java2面向对象编程-5包和classpath-1包package
查看>>
廖雪峰Java7处理日期和时间-3java.time的API-1LocalDateTime
查看>>
利用golang语法检查对象是否实现了接口
查看>>
在UBUNTU上安装基于bochs的 xv6
查看>>
Azure Storage Blob文件重命名
查看>>
RxJava2.0 使用
查看>>
FreeImage的图像处理软件
查看>>
ASP.NET MVC开发必看系列
查看>>
点到平面的距离
查看>>
linux下安装FTP
查看>>
第四周编程总结
查看>>
《第12章 类的无参方法》
查看>>
经典机器学习算法系列7-svd
查看>>