当前位置:网站首页>ES6 deconstruction assignment of array of deconstruction assignment
ES6 deconstruction assignment of array of deconstruction assignment
2022-04-22 12:28:00 【Yunmo - brother Kuang's blog】
Deconstruct assignment
< One > The deconstruction assignment is a JavaScript Expression, which makes it possible to extract values from an array , Or extract attributes from objects into different variables . ----MDN
< Two > Here's the explanation from the rookie tutorial
- Deconstructing assignment is an extension of assignment operator .
- It's a pattern match for an array or an object , And then assign values to the variables .
- Simple and easy to read in code writing , The meaning is clearer ; It is also convenient to obtain data fields in complex objects .
The following are the contents related to the deconstruction and assignment of some arrays
1. Simple deconstruction assignment
grammar const[ ] = [ ];
The array to the left of the equal sign is the variable to be assigned , The array on the right is the array to be deconstructed , Here's the code
const arr = [1,2,3,4];
let [a,b,c,d] = arr;
By this time a = 1; b=2; c=3; d= 4;
2. More complex deconstruction assignment
If we define a more complex array like the following line of code, how can we get the corresponding data
const arr = ['a','b',['c','d',['e','f','g']]];
Here are some points we can remember , namely
- Can be nested
- Negligible
- Incomplete deconstruction
- Remainder operator
- The default value is
For example, we need to get... In the last array b, We can do this
const arr = ['a','b',['c','d',['e','f','g']]];
const [,b] = arr;
We got... In the last array g, We can do this
const arr = ['a','b',['c','d',['e','f','g']]];
const [,,[,,[,,g]]]=arr;
Remainder operator , The following code uses the residual operator , Also called extension operator
What we should pay attention to here is , The remaining elements will be connected together in the form of an array in the original order
Operator must be placed last , Otherwise you will report an error
const arr = ['a','b',['c','d',['e','f','g']]];
const [a,...b] = arr;
Calculation results , By this time b

About deconstructing default values
1. When we deconstruct and assign values to an array , If the number of variables is more than the number of original array elements , The extra variables are directly copied as undefined
2. When the corresponding value of the variable is undfined The default value can be redefined when
for example
const arr = [1, undefined, undefined];
const [a, b = 2, c, d=“aaa”] = arr;
Now a,b,c,d The corresponding is 1,2,undefined,“aaa”;
Incomplete deconstruction
let [a = 1, b] = []; // a = 1, b = undefined
版权声明
本文为[Yunmo - brother Kuang's blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221225117664.html
边栏推荐
- 射频芯片在手机上起到什么作用?
- STM32 中的0UL或1UL是什么意思?
- Pytorch processes RNN input variable length sequence padding
- 【深入理解TcaplusDB技术】将数据插入到列表指定位置接口说明——[List表]
- 基于J2EE的房屋租赁系统的设计与实现.rar(论文+项目源码+数据库文件)
- Tencent cloud domain name binding
- [in depth understanding of tcallusdb technology] data interface description for reading the specified location in the list - [list table]
- What kind of SQL report can use the max – Pt function?
- 关于服务器 url大小写的问题
- 【深入理解TcaplusDB技术】示例代码——异步调用接口
猜你喜欢

LeetCode 83、删除排序链表中的重复元素

【并发编程054】多线程的状态及转换过程?

. net treasure API: outputformatter, format output object

LeetCode 695、岛屿的最大面积
![[concurrent programming 053] Why use volatile variables for double check lock variables](/img/d9/0a23a33c6573edaaa63ae588f13af9.png)
[concurrent programming 053] Why use volatile variables for double check lock variables
![[concurrent programming 052] talk about double check lock and its advantages?](/img/45/0d35b6b358f6605c46300f749dcd3d.png)
[concurrent programming 052] talk about double check lock and its advantages?
![[concurrent programming 051] implementation principle of volatile memory semantics](/img/48/0a42f47d44d1a9385d723252629170.png)
[concurrent programming 051] implementation principle of volatile memory semantics

ROS2学习笔记(五)从turtlesim学习ROS2参数

使用PHPstudy开启MySQL服务,并创建一个数据库
![[concurrent programming 054] multithreading status and transition process?](/img/b0/7f6bb898fb58477c73023135bd49db.png)
[concurrent programming 054] multithreading status and transition process?
随机推荐
What is the difference between CPU and GPU?
What does 0ul or 1ul in STM32 mean?
ROS2学习笔记(十)从turtlesim学习ROS2的工作空间
JS基础6
LeetCode 1768、交替合并字符串
[concurrent programming 052] talk about double check lock and its advantages?
CPU和GPU有什么区别?
LeetCode 389、找不同
使用PHPstudy开启MySQL服务,并创建一个数据库
MySQL 5.0安装教程图解详细教程
[concurrent programming 049] talk about the classification of reordering?
【深入理解TcaplusDB技术】示例代码——异步调用接口
What role does RF chip play in mobile phone?
ROS2学习笔记(八)从turtlesim学习ROS2的启动文件
Esp32-cam usage history
【并发编程049】说说重排序的分类?
NLP dataset collation (updating)
Setting policy of thread pool size
JS输出随机数
[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]