当前位置:网站首页>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
边栏推荐
- token 过期后,如何自动续期?
- MySQL事物
- Leetcode56. consolidation interval
- Official win 10 image download
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Learning automation ppt
- After getting these performance test decomposition operations, your test path will be more smooth
- Promise knowledge points
- About modifying dual system default startup item settings
- How to automatically renew a token after it expires?
猜你喜欢

I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?

8 ways to earn passive income

At the beginning of 2022, people who are ready to change jobs should pay attention to

Mysql database user management

MySQL transaction

Gestion des utilisateurs de la base de données MySQL

UE4_ Editor UMG close window cannot destroy UMG immediately

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

MySQL數據庫用戶管理
![[MD editing required] welcome to the CSDN markdown editor](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[MD editing required] welcome to the CSDN markdown editor
随机推荐
Who is promoting the new inflection point of audio and video industry in 2022?
[MD editing required] welcome to the CSDN markdown editor
Projet Web de déploiement du serveur Cloud
Solidity - Security - reentrancy attack
SHELL
Solidy - fallback function - 2 trigger execution modes
UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
What are membrane stress and membrane strain
Xctf attack and defense world crypto advanced area
MySQL数据库用户管理
Balanced binary tree judgment of Li Kou 110 -- classic problems
Leader: who can use redis expired monitoring to close orders and get out of here!
Summary of redis learning notes (I)
[exercise] basic practice letter graph of Blue Bridge Cup
STM32F103 series controlled OLED IIC 4-pin
Dao -- a beautiful new world?
[regular expression series] greedy and non greedy patterns
声网,站在物联网的“土壤”里
Acwing winter vacation daily question 2022 punch in day 11
Transfer the token on the matic-erc20 network to the matic polygon