const[a, b, c, d, e]='hello';
a // "h"
b // "e"
c // "l"
d // "l"
e // "o"--------------------------------//获取字符串的长度let{length : len}='hello';
len // 5
用途
两个变量的值交换或者多个进行交换
let x =1;let y =2;[x, y]=[y, x];
函数返回多个值后不需要遍历赋值了
// 返回一个数组functionexample(){return[1,2,3];}let[a, b, c]=example();
a //1
b //2
c //3------------------------------------------// 返回一个对象functionexample(){return{
foo:1,
bar:2};}let{ foo, bar }=example();
foo //1
bar //2
给函数传递数组或者函数
//传递对象用的最多,因为可以不受顺序限制// 参数是一组有次序的值functionf([x, y, z]){...}f([1,2,3]);// 参数是一组无次序的值functionf({x, y, z}){...}f({z:3, y:2, x:1});
提取json值
let jsonData ={
id:42,
status:"OK",
data:[867,5309]};let{ id, status, data: number }= jsonData;
console.log(id, status, number);// 42, "OK", [867, 5309]
遍历map解构
//先new一个map对象,给这个对象添加键值对//for,fo循环得到里面的键值对const map =newMap();
map.set('first','hello');
map.set('second','world');for(let[key, value]of map){
console.log(key +" is "+ value);}// first is hello// second is world