解構:
數組解構:
js的數組解構非常強大;
解構時,變量從左到右和元素對齊,可變參數放到最右邊;
能對應到數據就返回數據,對應不到數據就返回默認值,沒有默認值返回undefined;
例:
const arr = [1,3,5,7];
arr.push(9,11,13);
console.log(arr); //如何理解常量,常量,聲明和初始化不能分開,對象的地址(內存地址,數組首地址)不能變
// arr = 2; //X,TypeError: Assignment to constant variable.
輸出:
[ 1, 3, 5, 7, 9, 11, 13 ]
例:
const arr = [1,3,5];
// const x,y,z = arr; //X,是逗號表達式,另(x,y,z) = arr;語法錯誤;數組必須要用[]解構
const [x,y,z] = arr;
const [x,y,z,m,n] = arr; //多於數組元素
const [x,y] = arr; //少於數組元素;py必須兩邊個數對應
const [x,,,,,,,,m,n] = arr; //1 undefined undefined;py做不到
const [x,...m] = arr; //1 [ 3, 5 ];可變的要往後放
// const [x,...m,n] = arr; //X,SyntaxError: Rest element must be last element
,可變的要往後放
const [x,y,...m] = arr; //1 3 [ 5 ]
const [x=100,,,,,y=200] = arr; //支持默認值
例,嵌套數組:
const arr = [1,[2,3],4];
const [a,[b,c],d] = arr; //1 2 3 4
const [a,b] = arr; //1 [ 2, 3 ]
const [a,b,c,d=8] = arr; //1 [ 2, 3 ] 4 8
const [a,...b] = arr; //1 [ [ 2, 3 ], 4 ]
對象解構:
解構時,需要提供對象的屬性名,會根據屬性名找到對應的值,沒有找到的返回缺省值,沒有缺省值則返回undefined;
例,簡單對象:
const obj = {
a:100,
b:200,
c:300
};
let x,y,z = obj; //undefined undefined { a: 100, b: 200, c: 300 };X錯誤,是逗號表達式,
let {x,y,z} = obj; //undefined undefined undefined;找不到key
let {a,b,c} = obj; //V,按key來解
let {a,b,c,d} = obj; //100 200 300 undefined
let {a,x,c,d=400} = obj; //100 undefined 300 400
let {a,x=1000,c=3000,d=4000} = obj; //100 1000 300 4000
let {a:m,b:n,c} = obj; //100 200 300;重命名
console.log(m,n,c);
let {a:M,c:N,d:D='python'} = obj; //100 300 'python'
console.log(M,N,D);
例,嵌套對象:
var metadata = {
title: 'Scratchpad',
translations: [
{
locale: 'de',
localization_tags: [ ],
last_edit: '2018-10-22%16:42:00',
url: '/de/docs/Tools/Scratchpad',
title: 'JavaScript-Umgebung'
}
],
url: '/en-US/docs/Tools/Scratchpad'
};
var {title:enTitle, translations:[{title:localeTitle}]} = metadata;
console.log(enTitle,localeTitle);
輸出:
Scratchpad JavaScript-Umgebung
數組操作:
push(...items) //尾部增加多個元素;
pop() //移除最後一個元素,並返回它;
map //引入處理函數來處理數組中每一個元素,返回新的數組,可鏈式編程;
filter //引入處理函數處理數組中每一個元素,該處理函數將返回true的元素保留,將非true的元素過濾掉,保留的元素構成新的數組返回,可鏈式編程;
forEach //迭代所有元素,無返回值;
例:
const arr = [1,2,3,4,5];
arr.push(6,7,8,9,10);
console.log(arr);
arr.pop();
console.log(arr);
const power = arr.map(x=>x*x);
console.log(power);
const newarr = arr.filter(x=>x>5);
console.log(newarr);
const newarr1 = arr.forEach(x=>x+1); //無返回值
console.log(newarr1);
console.log(arr.forEach(x=>x+1),arr); //無返回值,沒有在原來數組上操作
輸出:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
[ 6, 7, 8, 9 ]
undefined
undefined [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
例:
const arr = [1,2,3,4,5],算出所有元素平方值是偶數,且大於10的結果;
console.log(arr.map(x=>x*x).filter(x=>x%2 === 0).filter(x=>x>10)); //不好
console.log(arr.filter(x=>x%2===0).map(x=>x*x).filter(x=>x>10)); //V,和DB中的查詢一樣,先過濾再計算
s = Math.sqrt(10);
console.log(arr.filter(x=>x>s && x%2 === 0).map(x=>x*x));
對象操作:
Object的靜態方法:
Object.keys(obj) //ES5開始支持,返回所有key
Object.values(obj) //返回所有值,試驗階段,支持較差
Ojbect.entries(obj) //返回所有值,試驗階段,支持較差
Object.assign(target,...sources) //使用多個source對象,來填充target對象,返回target對象
例:
const obj = {
a:100,
b:200,
c:300
};
console.log(Object.keys(obj));
console.log(Object.values(obj));
console.log(Object.entries(obj));
輸出:
[ 'a', 'b', 'c' ]
[ 100, 200, 300 ]
[ [ 'a', 100 ], [ 'b', 200 ], [ 'c', 300 ] ]
例:
var metadata = {
title: 'Scratchpad',
translations: [
{
locale: 'de',
localization_tags: [ ],
last_edit: '2018-10-22%16:42:00',
url: '/de/docs/Tools/Scratchpad',
title: 'JavaScript-Umgebung'
}
],
url: '/en-US/docs/Tools/Scratchpad'
};
var copy = Object.assign({},metadata,
{schoolName:'magedu',url:'www.magedu.com'},
{translations:null});
console.log(copy);
輸出:
{ title: 'Scratchpad',
translations: null,
url: 'www.magedu.com',
schoolName: 'magedu' }