解構(gòu)賦值
刪除對(duì)象不需要的屬性的值
const obj = {
name: 'detanx',
age: 24,
height: 180
}
// 刪除height
const { height, ...otherObj } = obj;
// otherObj => {name: 'detanx', age: 24}
交換兩個(gè)變量的值
let x = 1, y = 2;
[x, y] = [y, x]
// x: 2,y: 1
在解構(gòu)中使用別名
const object = { number: 10 }
const { number } = object
// 使用別名
const { number: otherNumber } = object
console.log(otherNumber) //10
字符串
判斷目標(biāo)字符是否包含某個(gè)字符串
let str1 = 'abcdefg';
let str2 = 'bcd';
// 一般寫法
if (str1.indexOf(str2) !== -1) {
return true;
} else {
return false;
}
// 優(yōu)雅寫法
return str1.includes(str2); // str1.includes(str2,2) //從角標(biāo)為2的位置開始判斷
字符串補(bǔ)全
// 從左邊開始補(bǔ)全,時(shí)間格式補(bǔ)全
'5'.padStart(2, '0'); // '05'
'04'.padStart(10, 'YYYY-MM-DD'); // 'YYYY-MM-04'
//從右邊補(bǔ)全
'abc'.padEnd(10, "foo"); // "abcfoofoof"
數(shù)組操作
復(fù)雜數(shù)組篩選
let arr1 = ['1', '2', '3', '4'];
let arr2 = [{id: '1', name: 'a'}, {id: '5', name: 'b'}, {id: '6', name: 'c'}];
let result;
// 一般寫法
arr1.forEach(item => {
arr2.forEach((obj) => {
if (item === obj.id) {
result = obj;
return;
}
})
});
// 優(yōu)雅寫法
arr1.some((item) => {
return result = arr2.filter(obj => obj.id === item);
})
console.log(result); // {id: '1', name: 'a'}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。