当前位置:网站首页>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
边栏推荐
- Ultra simple STM32 RTC alarm clock configuration
- Solidity - 安全 - 重入攻击(Reentrancy)
- 超简单 STM32 RTC闹钟 时钟配置
- Use of tornado template
- [secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
- Sword finger offer 22 The penultimate node in the linked list
- 领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
- [regular expression series] greedy and non greedy patterns
- Prototype and prototype chain in JS
- How to write a thesis
猜你喜欢
![[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point](/img/bb/ea0e78065ba54ff253995faeeb6901.png)
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point

聲網,站在物聯網的“土壤”裏

Intelligent question - horse racing question

How to create a CSR (certificate signing request) file?

Codeforces C. Andrew and Stones

What do you think of the deleted chat records? How to restore the deleted chat records on wechat?

MySQL事物

AI大模型落地大考,浪潮交出了怎样的答卷?

重构之美:当多线程批处理任务挑起大梁 - 万能脚手架

Master slave synchronization of MySQL database to realize read-write separation
随机推荐
如何制作CSR(Certificate Signing Request)文件?
Use of tornado template
MySQL日志管理、数据备份、恢复
token 过期后,如何自动续期?
2022年,谁在推动音视频产业的新拐点?
Title: enter two positive integers m and N to find their maximum common divisor and minimum common multiple
Mysql database user management
Related applications of priority queue
OpenCL线程代数库ViennaCL的使用
SSL证书续费相关问题详解
Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
MySQL數據庫用戶管理
1380. lucky numbers in matrices
InputStream to inputstreamsource
Force deduction exercise -- deleting repeated items in ordered sequence 1.0
Xctf attack and defense world crypto advanced area
Attempt to redefine 'timeout' at line 2 solution
Intelligent question - horse racing question
Sound net, debout dans le "sol" de l'IOT
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point