SOURCE

let [a, b, c] = [1, 2, 3];

console.log(a, b, c);

let [x, , y] = [1, 2, 3];
console.log(x, y);

let [head, ...tail]=[1,2,3,4]
console.log(head,tail);

let [x1,y1,z1]=new Set(['a','b','c']);
console.log(x1,y1,z1)
//默认值
let [x2,y2='b']=['a'];
console.log(x2,y2);

//对象的解构赋值
//对象的属性没有次序,变量必须与属性同名,才能取到正确的值.
let {bar,foo}={foo:'aaa',bar:'bbb'};
console.log(foo,bar)

let{log,sin,cos}=Math;
console.log(cos(Math.PI))

//对象的解构也可以指定默认值
var {message:msg='Something went wrong'}={}
console.log(msg);

//字符串能解构成数组
const [a1,b1,c1,d1,e1]='hello';
console.log(a1,b1,c1,d1,e1)

//用途--从函数返回多个值
function example(){
    return [1,2,3];
}
let [a2,b2,c2]=example();
console.log(a2,b2,c2);

//用途--提取JSON数据
let jsonData={
    id:42,
    status:"OK",
    data:[867,5309]
};

let {id,status,data:number}=jsonData;
console.log(id,status,number)

//用途--遍历Map解构
const map=new Map();
map.set('first','hello');
map.set('second','world');

for(let [key,value] of map){
    console.log(key,value)
}


console 命令行工具 X clear

                    
>
console