三元運算符 基礎: let hungry = true; let eat; if (hungry true) { eat = 'yes'; } else { eat = 'no'; } 高階: let hungry = true; let eat = hungry true ? 'yes' : 'n ...
三元運算符
基礎:
let hungry = true;
let eat;
if (hungry === true) {
eat = 'yes';
} else {
eat = 'no';
}
高階:
let hungry = true;
let eat = hungry === true ? 'yes' : 'no';
數字到字元串,字元串到數字
基礎:
let num = 15;
let s = num.toString(); // number to string
let n = Number(s); // string to number
高階:
let num = 15;
let s = num + ""; // number to string
let n = +s; // string to number
填充數組
基礎:
for(let i=0; i < arraySize; i++){
filledArray[i] {'hello' : 'goodbye'};
}
高階:
let filledArray = new Array(arraysize).fill(null).map(()=> ({'hello' : 'goodbye'}));
對象中的動態屬性
基礎:
let dynamic = "value";
let user = {
id: 1,
};
user[dynamic] = "other value";
高階:
let dynamic = "value";
let user = {
id: 1,
[dynamic]: "other value"
};
刪除重覆項
基礎:
let array = [100, 23, 23, 23, 23, 67, 45];
let outputArray = [];
let flag = false;
for (j = 0; < array.length; j++) {
for (k = 0; k < outputArray.length; k++) {
if (array[j] == outputArray[k]) {
flag = true;
}
}
if (flag == false) {
outputArray.push(array[j]);
}
flag = false;
}
//outputArray = [100, 23, 67, 45]
高階:
let array = [100, 23, 23, 23, 23, 67, 45];
let outputArray = Array.from(new Set(array));
//outputArray = [100, 23, 67, 45]
數組轉對象
基礎:
let arr = ["value1", "value2", "value3"];
let arrObject = {};
for (let i = 0; i < arr.length; ++i) {
if (arr[i] !== undefined) {
arrObject[i] = arr[i];
}
}
高階:
let arr = ["value1", "value2", "value3"];
let arrObject = {...arr};
對象轉數組
基礎:
let number = {
one: 1,
two: 2,
};
let keys = [];
for (let numbers in numbers) {
if (number.hasOwnProperty(number)) {
keys.push(number);
}
}
// key = [ 'one', 'two' ]
高階:
let numbers = {
one: 1,
two: 2,
};
let key = Object.keys(numbers); // key = [ 'one', 'two' ]
let value = Object.values(numbers); // value = [ 1, 2 ]
let entry = Object.entries(numbers); // entry = [['one' : 1], ['two' : 2]]
條件短路
基礎
if (docs) {
goToDocs();
}
高階
docs && goToDocs()
使用 ^ 檢查數字是否不相等
//基礎
if(a!=123)
//高階
if(a^123)
迴圈訪問對象
const age = {
Rahul: 20,
max: 16
};
// Solution 1 - Get 'keys' and loop over
const keys = Object.keys(age);
keys.forEach(key => age[key]++);
con
sole.log(age); // { Rahul: 21, max: 17 }
// Solution 2 - for ..in loop
for(let key in age){
age[key]++;
}
console.log(age); // { Rahul: 22, max: 18 }
Object keys 按插入順序存儲
const obj = {
name: "Rahul",
age: 16,
address: "Earth",
profession: "Developer",
};
console.log(Object.keys(obj)); // name, age, address, profession
檢查值是數組
const arr = [1, 2, 3];
console.log(typeof arr); // object
console.log(Array.isArray(arr)); // true
初始化大小為n的數組,並用預設值填充
const size = 5;
const defaultValue = 0;
const arr = Array(size).fill(defaultValue);
console.log(arr); // [0, 0, 0, 0, 0]
雙等和三等的區別
//雙等號(==)只比較值是否相等。
//它本質上是做類型轉換。這意味著在比較數值之前,它將變數的類型轉換為相互匹配。
console.log(0 == '0'); // true
// 另一方面,三等號(===)不執行類型轉換。
// 它將驗證被比較的變數是否具有相同的值和相同的類型。
console.log(0 === '0'); // false
接收更好的傳參方式
function downloadData(url, resourceId, searchTest, pageNo, limit) {}
downloadData(...); // 需要記住傳參順序
更簡單的方法:
function downloadData(
{ url, resourceId, searchTest, pageNo, limit } = {}
) {}
downloadData(
{ resourceId: 2, url: "/posts", searchText: "WebDev" }
);
null 和 undefined
null 是一個 value,然而 undefined 不是.
null 像一個空盒子,但 undefined 不是.
const fn = (x = 'default value') => console.log(x);
fn(undefined); // default value
fn(); // default value
fn(null); // null
傳遞 null 時,不採用預設值。然而,當未定義或未傳遞任何內容時,將採用預設值。