Generates random combinations of letters and arrays
Math.random().toString(36).substr(2);
Copy the code
Formatting time
const dateFormatter = (formatter, date) = > {
date = (date ? new Date(date) : new Date)
const Y = date.getFullYear() + ' ',
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds()
return formatter.replace(/YYYY|yyyy/g, Y)
.replace(/YY|yy/g, Y.substr(2.2))
.replace(/MM/g, (M < 10 ? '0' : ' ') + M)
.replace(/DD/g, (D < 10 ? '0' : ' ') + D)
.replace(/HH|hh/g, (H < 10 ? '0' : ' ') + H)
.replace(/mm/g, (m < 10 ? '0' : ' ') + m)
.replace(/ss/g, (s < 10 ? '0' : ' ') + s)
}
dateFormatter('YYYY/MM/DD hh:mm:ss'.new Date()) / / 2021/06/21 11:11:17
Copy the code
Gets the actual type of the variable
const trueTypeOf = (obj) = > {
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
};
console.log(trueTypeOf(' '));
// string
console.log(trueTypeOf(0));
// number
console.log(trueTypeOf());
// undefined
console.log(trueTypeOf(null));
// null
console.log(trueTypeOf({}));
// object
console.log(trueTypeOf([]));
// array
console.log(trueTypeOf(0));
// number
console.log(trueTypeOf(() = > {}));
// function
Copy the code
1. If multiple conditions judge
/ / redundancy
if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') {}
/ / concise
if (['abc'.'def'.'ghi'.'jkl'].includes(x)) {}
Copy the code
2. if… else…
/ / redundancy
let test: boolean;
if (x > 100) {
test = true;
} else {
test = false;
}
/ / concise
let test = x > 10;
let text = x > 10 ? true : false
Copy the code
- Multiple conditions determine the same execution method
if(status === 0 || status === 1 || status === 2 || status === 3) {
console.log('Button can be clicked');
}
/ / = >
if([0.1.2.3].includes(status)) {
console.log('Button can be clicked');
}
Copy the code
- There are multiple possibilities for different situations
// status Is not a value or the value is too large
const statusTextObject = {
100: 'Deleted'.101: 'not started'.102: 'In class'.103: 'Class dismissed'.104: 'Assessed'
}
text = statusTextObject[status] || The '-';
// Replace the key of the object with the value of status.
Copy the code
3. Check for Null, Undefined, and Null values
/ / redundancy
if(first ! = =null|| first ! = =undefined|| first ! = =' ') {
let second = first;
}
/ / concise
let second = first || ' ';
Copy the code
4. Function conditional call
/ / redundancy
function test1() {
console.log('test1');
};
function test2() {
console.log('test2');
};
var test3 = 1;
if (test3 == 1) {
test1();
} else {
test2();
}
/ / simple
(test3 === 1? test1:test2)();
Copy the code
5. Switch multiple conditions
/ / redundancy
switch (data) {
case 1:
test1();
break;
case 2:
test2();
break;
case 3:
test();
break;
// so on...
}
/ / concise
var data = {
1: test1,
2: test2,
3: test
};
data[anything] && data[anything]();
Copy the code
6. Implicit return
/ / redundancy
function getArea(diameter) {
return Math.PI * diameter
}
/ / concise
getArea = diameter= > (
Math.PI * diameter;
)
Copy the code
7. Plain array de-duplication & object array de-duplication
const uniqueArr = (arr) = > [...new Set(arr)];
console.log(uniqueArr([1.2.3.1.2.3.4.5]));
// [1, 2, 3, 4, 5]
const uniqueElementsBy = (arr, fn) = >arr
.reduce((acc, v) = > {if(! acc.some(x= > fn(v, x))) acc.push(v);return acc;}, []);
uniqueElementsBy([{id: 1.name: 'Jhon'}, {id: 2.name: 'sss'},
{id: 1.name: 'Jhon'}].(a, b) = > a.id == b.id)
// [{id: 1, name: 'Jhon'}, {id: 2, name: 'sss'}]
Copy the code
8. Fetch the value of the corresponding property from the array
const reducedFilter = (data, keys, fn) = >data.filter(fn)
.map(el= >keys.reduce((acc, key) = > {acc[key] =el[key];returnacc; }, {}));const data = [
{
id: 1.name: 'john'.age: 24
},
{
id: 2.name: 'mike'.age: 50}];let a = reducedFilter(data, ['id'.'name'].item= > item.age > 24); // [{ id: 2, name: 'mike'}]
Copy the code
9. Return cur.push(v*2),cur
Let arr = [1, 2, 3, 4] const b = arr. Reduce ((cur, v) = > [... cur, v * 2], []) const b1 = arr. Reduce ((cur, v) = > {/ / return directly Cur.push (v*2) returns an error because push does not return an array, Return cur.push(v*2),cur},[]) const b2=arr. Reduce ((res, cur)=> {res.push(cur *2); return res; }, []) / /,4,6,8 [2]Copy the code
return cur.push(v*2),cur
Is equivalent toreturn cur