当前位置:网站首页>ES6解构赋值
ES6解构赋值
2022-06-30 05:58:00 【SignalFire】
解构赋值:按照一定模式从数组和对象中提取值对变量进行赋值。
一、数组解构
数组解构赋值时按照索引进行对应赋值,要注意顺序
(1)一维数组
let arr = [1,2,3];
let [a,b,c] = arr;
console.log(a,b,c);//1,2,3
(2)二维数组
let arr = [1,2,[3,4]];
let [a,b,c] = arr;
console.log(a,b,c);//1 2 (2) [3, 4]
let arr = [1,2,[3,4]];
let [a,b,[c]] = arr;
console.log(a,b,c);//1 2 3
(3)左侧接受的变量在右边没有对应的
let arr = [1,2,[3,4]];
let [a,b,[c],d] = arr;
console.log(a,b,c,d);//1 2 3 undefined
(4)设置默认值
let arr = [1,2,[3,4]];
let [a,b,[c],d = 5] = arr;
console.log(a,b,c,d);//1 2 3 5
二、对象解构
按照对象的键赋值,顺序随意
(1)
let hero = {
name:"Asia",
age:23
}
let {name,age} = hero;
console.log(name,age);//Asia 23
左侧换顺序
let hero = {
name:"Asia",
age:23
}
let {age,name} = hero;
console.log(name,age);//Asia 23
(2) 定义别名
let hero = {
name:"Asia",
age:23
}
let {age:age2,name:name2} = hero;
console.log(name2,age2);//Asia 23
三、字符串解构赋值
和数组一样按照顺序解构
let str = "面对疾风吧!哈撒给!"
let [a,b,c,d,e,f,g,h,i,j,k,l,m,n] = str;
console.log(a,b,c,d,e,f,g,h,i,j,k,l,m,n);//面 对 疾 风 吧 ! 哈 撒 给 ! undefined undefined undefined undefined
四、函数参数解构赋值
function fn([a,b,c]){
console.log(a,b,c);
}
fn([1,2,3]);//1 2 3
五、JSON解构赋值
let json = '{"name":"Asia","age":20}';
let {name,age} = JSON.parse(json);
console.log(name,age);//Asia 20
边栏推荐
- Solidity - 安全 - 重入攻击(Reentrancy)
- Projet Web de déploiement du serveur Cloud
- Tornado frame foundation
- [deep learning] data segmentation
- UE4_ Editor UMG close window cannot destroy UMG immediately
- English grammar_ Adjective / adverb Level 3 - superlative
- [Alibaba cloud] student growth plan answers
- Sword finger offer 22 The penultimate node in the linked list
- Solitidy - fallback 回退函数 - 2种触发执行方式
- MySQL事物
猜你喜欢
![[GPU] basic operation](/img/76/6b22368e3addd30aef1dd2258ee49a.jpg)
[GPU] basic operation

Solidity - 安全 - 重入攻击(Reentrancy)

MySQL数据库用户管理

Configure the user to log in to the device through telnet -- AAA local authentication

MySQL 索引

C语言基础小操作
![[road of system analyst] collection of wrong topics in Project Management Chapter](/img/8b/2908cd282f5e505efe5223b4c5ddbf.jpg)
[road of system analyst] collection of wrong topics in Project Management Chapter
![09- [istio] istio service entry](/img/48/86f8ec916201eefc6ca09c45a60a6a.jpg)
09- [istio] istio service entry

Leetcode search insert location

MySQL log management, data backup and recovery
随机推荐
The average salary of software testing in 2022 has been released. Have you been averaged?
Do you know how to show the health code in only 2 steps
How to create a CSR (certificate signing request) file?
8 ways to earn passive income
hashlips_ art_ Engine-1.0.6 usage
Codeforces C. Andrew and Stones
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
Mysql database user management
Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
Use of OpenCL thread algebra library viennacl
STM32F103系列控制的OLED IIC 4针
网络基础知识
Use of tornado template
Sword finger offer 18 Delete the node of the linked list
OSPF - authentication and load balancing summary (including configuration commands)
Codeforces Round #390 (Div. 2) D. Fedor and coupons
如何制作CSR(Certificate Signing Request)文件?
MySQL index
About modifying dual system default startup item settings