当前位置:网站首页>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
边栏推荐
- ES6数组遍历与ES5数组遍历
- MySQL存储系统
- [ansible series] fundamentals 02 module debug
- 从零开发 stylelint规则(插件)
- Shenzhou ares tx6 boot logo modification tutorial
- [database] transaction
- Mariadb数据库的安装与初始化
- Mysql database user management
- Decompilation normal decompilation problems. Solve them yourself
- Leetcode search insert location
猜你喜欢
随机推荐
Common address collection
Official win 10 image download
Common mistakes daily practice 01
One sentence introduction to Trojan horse
Intelligent deodorizer embedded development
IP TCP UDP network encryption suite format
重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
Decompilation normal decompilation problems. Solve them yourself
【LeetCode】236. Nearest common ancestor of binary tree
观察者模式、状态模式在实际工作中的使用
24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
[MD editing required] welcome to the CSDN markdown editor
Es6数组
雲服務器部署 Web 項目
超简单 STM32 RTC闹钟 时钟配置
Résoudre le problème de décompiler la compilation normale
飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime
Cisco vxlan configuration
关于Glide加载图片模糊不清楚
[openstack]-01- basic introduction









