三元运算符
基础:
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 时,不采用默认值。然而,当未定义或未传递任何内容时,将采用默认值。
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!