当前位置:网站首页>ES6 deconstruction assignment
ES6 deconstruction assignment
2022-06-30 06:05:00 【SignalFire】
Deconstruct assignment : Extract values from arrays and objects according to a certain pattern to assign values to variables .
One 、 An array of deconstruction
When the array is deconstructed and assigned, the corresponding assignment is performed according to the index , Pay attention to the order
(1) One dimensional array
let arr = [1,2,3];
let [a,b,c] = arr;
console.log(a,b,c);//1,2,3
(2) Two dimensional array
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) The variables accepted on the left have no corresponding variables on the right
let arr = [1,2,[3,4]];
let [a,b,[c],d] = arr;
console.log(a,b,c,d);//1 2 3 undefined
(4) Set the default value
let arr = [1,2,[3,4]];
let [a,b,[c],d = 5] = arr;
console.log(a,b,c,d);//1 2 3 5
Two 、 Object to deconstruct
Assign values according to the key of the object , Random order
(1)
let hero = {
name:"Asia",
age:23
}
let {name,age} = hero;
console.log(name,age);//Asia 23
Left shift sequence
let hero = {
name:"Asia",
age:23
}
let {age,name} = hero;
console.log(name,age);//Asia 23
(2) Define an alias
let hero = {
name:"Asia",
age:23
}
let {age:age2,name:name2} = hero;
console.log(name2,age2);//Asia 23
3、 ... and 、 String deconstruction assignment
Deconstruct in the same order as an array
let str = " Face the wind ! Hassa gave !"
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);// Noodles Yes disease wind Well ! Ha scatter to ! undefined undefined undefined undefined
Four 、 Function parameter deconstruction assignment
function fn([a,b,c]){
console.log(a,b,c);
}
fn([1,2,3]);//1 2 3
5、 ... and 、JSON Deconstruct assignment
let json = '{"name":"Asia","age":20}';
let {name,age} = JSON.parse(json);
console.log(name,age);//Asia 20
边栏推荐
- One sentence introduction to Trojan horse
- STM32F103 series controlled OLED IIC 4-pin
- Résoudre le problème de décompiler la compilation normale
- Force deduction exercise -- deleting repeated items in ordered sequence 1.0
- MySQL transaction
- 反編譯正常回編譯出現問題自己解决辦法
- JS prototype chain object function relationship
- UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
- Official win 10 image download
- Common NPM install errors
猜你喜欢

Summation of basic exercise sequence of test questions

接口中方法详解

Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?

Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries

Tornado frame foundation

Implementation of property management system with ssm+ wechat applet

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

Voting vault: a new primitive for defi and Governance

【LeetCode】236. Nearest common ancestor of binary tree

SparseArray
随机推荐
Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
MySQL數據庫用戶管理
Inno setup the simplest user-defined interface effect
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
[ansible series] fundamentals 02 module debug
Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime
RSA and AES
583. 两个字符串的删除操作-动态规划
UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
[GPU] basic operation of GPU (I)
文件操作IO-Part1
1380. lucky numbers in matrices
Tornado frame foundation
Luogup2756 pilot pairing scheme problem (maximum flow)
Decompilation normal decompilation problems. Solve them yourself
Finally someone can make the server so straightforward
ES6箭头函数
8 ways to earn passive income