var emps = [
{ ename: "莉莉", age: 32, phone: '13599898888' },
{ ename: "李军", age: 22, phone: '13544498888' },
{ ename: "程程", age: 33, phone: '13599333888' },
{ ename: "小袁", age: 36, phone: '13599777888' },
]
1.普通解构(emp1为自己为数组中的第一个对象起的名字)
const [emp1,emp2 ,emp3 , emp3] = emps
2.可选解构
(1)按顺序解构
const [emp1, emp] = emps
(2)跳过中间解构,跳过但是需要留位置
const [emp1, , , emp4] = emps
console.log(emp1.ename, emp4.ename)
3.复杂解构
var paopao = {
name: "李华",
age: 18,
tags: ['html', 'css', 'js'],
husband: {
name: "大连",
age: 26
}
}
解构
var {
name,
age,
husband: { age: hus_age, name: hus_name },
tags: [tag1, tag2, tag3]
} = paopao
总结:
var emp = {ename: "李华", age: 32, phone: "18879798888"}
// 别名: 换个名字
// { 属性名: 别名 }
var { ename: en } = emp
console.log('en:', en)
// 解构
var { ename, age, phone } = emp
console.log(ename, age, phone)
// 期望:
var ename = emp.ename
var age = emp.age
var phone = emp.phone
解构语法缺点:
1.把值赋给变量时,格式略长
2.当数组中有多个对象时,无法直观的知道其中对象的下标 0 和 1 是什么