模式匹配賦值,左邊是陣列,右邊也是陣列,一一對應賦值
例子:
let [a,b,c]=[1,2,3]
解構賦值允許指定預設值,只有當乙個陣列成員===嚴格等於undefined,預設值才會生效
字串的結構賦值
字串也可以解構賦值,這是因為此時,字串被轉換成了乙個類似陣列的物件
例子:
const [a,b,c,d,e]='hello';
a//『h』
b//『e』
c//『l』
d//『l』
e//『o』
函式引數的結構賦值
例子:
var aa=[[1,2],[3,4],[5,6]];
var bb=aa.map(function([a,b]));
console.log(bb);
//arr陣列裡面是物件
arr.map(function(item,index))
//arr陣列裡面是陣列
arr.map(function([a,b],index))
用途:
1.交換變數的值
例子:
let x=1;
let y=2;
[x,y]=[y,x];
2.從函式中返回多個值
function example()
let [a,b,c]=example();
3.函式引數的定義
function f([x,y,z])
console.log(f([1,2,3]))
4.提取json資料(這個感覺很實用)
解構賦值對提取json物件中的資料
let jsondata=
let =jsondata;
console.log(id);
console.log(status);
console.log(data);
5.函式引數的預設值
6.遍歷map解構
map是乙個物件,鍵值對
const map=new map();
map.set('first','hello');
map.set('second','world');
for(let [key,value] of map)
//let of 遍歷陣列 let in 好像只能取到陣列的下標
var student=[,];
for(let item of student)
筆記: ES6筆記(解構)
1 解構 解構通俗點說,就是通過一種特定格式,快捷的讀取物件 陣列中的資料的方法 基本用法 如果右邊是物件,左邊也要用物件的格式,解構出來就是變數了,再也不是屬性了 解構物件 var ouser es5讀資料的方法 console.log ouser name ouser age aaa 20 le...
es6變數解構賦值
es6允許按照一定模式,從陣列和物件中提取值,對變數進行賦值,這被稱為解構。解構賦值在實際開發中可以大量減少我們的 量,並且讓我們的程式結構更清晰。陣列的解構賦值 let a,b 1 2 console.log a 1 console.log b 2 上面的 表示,可以從陣列中提取值,按照位置的物件...
ES6變數解構賦值
es6 允許按照一定模式,從陣列和物件中提取值,對變數進行賦值,這被稱為解構 es6之前我們申明多個變數需要按照下面的方法 let l a 1 let b 2 let c 3 let d 4 或者 let a 1,b 2,c 3,d 4 現在我們可以更加簡便 let a,b,c,d 1,2,3,4 ...