直接上代码
{ let JSONData = { title:'abc', test:[ { nums:5, name:'jobs' }, { nums:11, name:'bill' } ] } let {title:estitle,test:[num,names]} = JSONData; console.log(estitle,num,names); } //得到abc { nums: 5, name: 'jobs' } { nums: 11, name: 'bill' }
解构赋值还可以用在交换变量值上
let a=1;let b = 2; [a,b] = [b,a]; ==》b=1,a=2;
还可以用在接受参数上
当只想接收某些返回值的时候
例:function f(){
return [1,3,5,7];
}
只想接受1,7
let a,b,c,d;
[a,,,b]=f();
[a,,...d] = f();