当前位置:网站首页>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
边栏推荐
- Swoole process model diagram
- What indicators should safety service engineers pay attention to in emergency response?
- IP TCP UDP network encryption suite format
- 重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
- VLAN access mode
- RSA and AES
- Decompilation normal decompilation problems. Solve them yourself
- Implementation of property management system with ssm+ wechat applet
- MySQL advanced SQL statement
- ES6数组遍历与ES5数组遍历
猜你喜欢
[ansible series] fundamentals -01
At the beginning of 2022, people who are ready to change jobs should pay attention to
Verilog中case,casez,casex语句的用法
Balanced binary tree judgment of Li Kou 110 -- classic problems
Do you know how to show the health code in only 2 steps
01. 正则表达式概述
1380. lucky numbers in matrices
谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
文件操作IO-Part1
SHELL
随机推荐
Cisco VXLAN配置
Common address collection
After getting these performance test decomposition operations, your test path will be more smooth
Share problems solved
接口中方法详解
重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
Summation of basic exercise sequence of test questions
MySQL summary
从底层结构开始学习FPGA----RAM IP核及关键参数介绍
Golang之手写web框架
反編譯正常回編譯出現問題自己解决辦法
VIM view file code
Codeforces C. Andrew and Stones
【数据库】事务
MySQL高级SQL语句
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
InputStream to inputstreamsource
Prototype and prototype chain in JS
Finally someone can make the server so straightforward
[regular expression series] greedy and non greedy patterns