当前位置:网站首页>ES6 new - deconstruction assignment of array / object
ES6 new - deconstruction assignment of array / object
2022-07-27 09:12:00 【Cabbage 007】
Catalog
2. Deconstruction assignment summary :
1.es6, An array of deconstruction
2. Deconstruction supports nesting , The left and right structures correspond
4. Assign default , The deconstructed variables are undefined, The default value will take effect
5. matters needing attention :
3. Rename the deconstructed variable name
Deconstruct assignment
1. Definition :
es6 Allow to follow certain mode , Extract... From arrays and objects , Assign values to variables , This is called deconstruction .
2. Deconstruction assignment summary :
2.1. The structure of the left and right sides of the assignment symbol must be consistent ( Or it's all arrays , Or they are all objects )
2.2. The declaration and assignment of variables must be completed in one sentence .
An array of deconstruction
let arr = [ " durant ",33,"NBA Professional players " ]
es5:
let lastname = arrr[0]
let age = arr[1]
let job = arr[2]
1.es6, An array of deconstruction
let [lastname,age,job ] = arr
console.log(lastname,age,job)
![]()
2. Deconstruction supports nesting , The left and right structures correspond
let arr = [ " durant ",[33],"NBA Professional players " ]
let [lastname, [age] ,job ] = arr
console.log(age)
![]()
3. Partial deconstruction
let arr = [ " durant ",33,"NBA Professional players "," The nets " ]
Partial deconstruction , The previous part uses commas to indicate skipping
let [,age,job] = arr
console.log(age,job)
![]()
... Receive the rest , Inside this rest It's a Array
let [lastname,...rest] = arr
console.log(lastname,rest)
![]()
4. Assign default , The deconstructed variables are undefined, The default value will take effect
let arr = [' durant ',33]
let [,,team] = arr
console.log(arr)
![]()
let [,,team = " The thunder "] = arr
console.log(team)
![]()
let arr = [ " durant " ,33," The nets "]
let [,,team = " The thunder "] = arr
console.log(team)
![]()
5. matters needing attention :
let arr = [' durant ',33]
5.1 The left and right deconstructions are inconsistent => undefined
let [lastname,age ] = arr
console.log(lastname,age)
![]()
5.2 Declaration and assignment are not completed in one sentence => Report errors
let [ lastname,age ]
[ lastname,age ] = arr
Object to deconstruct
let obj = { lastname:" durant ",age:"33",job:"NBA Professional players " }
es5
let lastname = obj[ "lastname" ]
let age = obj.age
let job = obj.job
1.es6, Object to deconstruct
let { lastname,age,job } = { lastnaem:" durant ",age:33,job:"NBA Professional players " }
//let { age,job lastname } = { lastnaem:" durant ",age:33,job:"NBA Professional players " } // The object is disordered
// let { lastname:lastname,age:age,job:job} = { lastnaem:" durant ",age:33,job:"NBA Professional players " }
console.log( lastname,age,job )
![]()
2. Deconstruction supports nesting , The left and right structures correspond , Grasp the strength of the structure
let obj = {
name:{ lastname:" durant ",nickname:[" contractor "," Du Xiaoshuai "] },
age:33,job:"NBA Professional players " }
// Take out lastname,nickname,age,job Value
let { name:{lastname,nickname},age,job} = obj
console.log( lastname,nickname,age,job )
![]()
// Yes nickname The array is deconstructed again
let { name:{lastname,nickname},age,job} = obj
let [name1,name2] = nickname
console.log(name1,name2)
![]()
3. Rename the deconstructed variable name
let obj = { lastname:" durant ",age:"33",job:"NBA Professional players " }
let { lastname:uesrname ,age } = obj
console.log(username,age)
![]()
4. Partial deconstruction
let obj = { lastname:" durant ",age:"33",job:"NBA Professional players ",team:" The nets " }
... Receive the rest , there rest It's a object
let { lastname,...rest } = obj
console.log(lastname,rest)
![]()
5. The default value is , The deconstructed variable value is undefined, The default value will take effect
let obj = { lastname:" durant ",age:"33" }
let { lastname,team} = obj
console.log(lastname,team)
![]()
let { lastname,team = " The thunder " } = obj
console.log( lastname,team )
![]()
let obj = { lastname:" durant ",age:"33",team:" The nets " }
let { team = " The thunder ",no:num = 7 } = obj
console.log( team,num)
![]()
6. matters needing attention
let obj = { lastname:" durant ", age:33,team:" The nets "}
6.1 The left and right structures are inconsistent => Report errors
let [lastname,age] = obj
console.log(lastname,age)

6.2 Declaration and assignment are not completed in the same sentence => complete
let {lastname,age}
{lastname,age} = obj

7. application
7.1 Instead of object . Property name The value of
7.2. The structure of function parameters , simplify object ... Property name The value of
function add (obj){ return obj.x+obj.y }
consle.log(add( { x:12,y:24 } ))
// Abbreviation
function add ({ x,y }){ return x+y }
consle.log(add( { x:12,y:24 } ))
边栏推荐
- How to study happily on station B?
- Qdoublevalidator does not take effect solution
- Nut joke based on arkui ETS
- 存储和计算引擎
- QDoubleValidator不生效问题解决办法
- flex:1的原理
- BOM的常用操作和有关获取页面/窗口高度、宽度及滚动的兼容性写法
- Ctfshow ultimate assessment
- 被三星和台积电挤压的Intel终放下身段,为中国芯片定制芯片工艺
- Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
猜你喜欢

Five kinds of 3D attention/transformer finishing (a-scn, point attention, CAA, offset attention, point transformer)

Music experience ceiling! Emotional design details of 14 Netease cloud music

Antdesign a-modal自定义指令实现拖拽放大缩小

B tree

5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了

C# 窗体应用常用基础控件讲解(适合萌新)

8 kinds of visual transformer finishing (Part 2)

Ctfshow ultimate assessment

flex布局 (实战小米官网)

vscod
随机推荐
QT uses SQLite to open multiple database files at the same time
CUDA Programming -03: thread level
ArkUI中的显式动画
Qt中发送信号时参数为结构体或者自定义的类怎么办?
Intel, squeezed by Samsung and TSMC, finally put down its body to customize chip technology for Chinese chips
Mmrotate trains its dataset from scratch
微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
NPM install error forced installation
Activation functions commonly used in deep learning
Huawei machine test question: String transformation minimum string JS
[cloud native kubernetes practice] deploy the rainbow platform under the kubernetes cluster
Ztree custom title attribute
npm install报错 强制安装
500 error reporting
ArkUI框架中的两个小技巧
HUAWEI 机试题:火星文计算 js
Babbitt | yuan universe daily must read: Guangzhou Nansha released the "Yuan universe nine" measures, and the platform can obtain up to 200million yuan of financial support
Easy language programming: allow the screen reading software to obtain the text of the label control
Explanation of binary tree
【进程间通信IPC】- 信号量的学习