当前位置:网站首页>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 } ))
边栏推荐
- 8 kinds of visual transformer finishing (Part 2)
- Five kinds of 3D attention/transformer finishing (a-scn, point attention, CAA, offset attention, point transformer)
- 巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
- Qt中发送信号时参数为结构体或者自定义的类怎么办?
- Data interaction based on restful pages
- 【ACL2020】一种新颖的成分句法树序列化方法
- 5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises
- SQL exercise set
- 坚果天气
- 5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了
猜你喜欢

被三星和台积电挤压的Intel终放下身段,为中国芯片定制芯片工艺

8 kinds of visual transformer finishing (Part 2)

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

500 error reporting

Low cost, low threshold, easy deployment, a new choice for the digital transformation of 48 million + small and medium-sized enterprises

Data interaction based on restful pages

PyTorch自定义CUDA算子教程与运行时间分析

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

"Weilai Cup" 2022 Niuke summer multi school training camp 1

Deep understanding of Kalman filter (2): one dimensional Kalman filter
随机推荐
二叉树讲解
Matlab solves differential algebraic equations (DAE)
Replace restricts the text box to regular expressions of numbers, numbers, letters, etc
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
Sharing of four open source face recognition projects
Matlab uses m file to produce fuzzy controller
Tensorflow模型训练和评估的内置方法
CUDA programming-04: CUDA memory model
Encountered 7 file(s) that should have been pointers, but weren‘t
Common operations of BOM and compatible writing methods for obtaining page / window height, width and scrolling
JS detects whether the client software is installed
Tensorflow loss function
[interprocess communication IPC] - semaphore learning
flex:1的原理
8 kinds of visual transformer finishing (Part 1)
MySQL basic knowledge learning (I)
The lifecycle of arkui development framework components
Explanation of common basic controls for C # form application (suitable for Mengxin)
【每日算法Day 96】腾讯面试题:合并两个有序数组
Nutrecipes developed based on arkui ETS