1. Array deduplication
letArr =,2,2,3,3,4,5,6 [1]letNewArr = [... new Set (arr)] / [6] / / or Array. The from (new Set (arr))Copy the code
2. Cast String to Number
'123'* 1 / / 123'china' * 1 // NaN
null * 1 // 0
undefined * 1 // NaN
Copy the code
+ '666'/ / 666 +' ' // 0
+ null // 0
+ 'china' // NaN
+ undefined // NaN
Copy the code
When + is interpreted as a join operator rather than an addition operator, two ~~ substitutions can be used. For integers ~~ yields the same result as math.floor () (a tilde ~, called the “bitwise inverse operator”, is equivalent to -n-1. So ~15 = -16.)
3, includes
letArr = [1,2,3,4,5] console.log(arr.includes(1) == Arr.indexof (1)! / / = 1)true
Copy the code
Determine whether an element exists in a string or array using includes instead of indexOf
4, URLSearchParams
Basic usage
let url = new URLSearchParams(location.search.slice(1));
Copy the code
Using URL construction
let location = 'http://www.baidu.com?id=2'
let _url = (new URL(location)).searchParams;
console.log(_url.get("id"/ / 2))Copy the code
Gets/sets parameters
- Get (key) : obtains parameters
- Set (key) : sets parameters
- Append (key, value) :
5. Objects are de-merged
Object.assign
var obj1 = { a : 1 };
var obj2 = { b : 2 };
var obj3 = { c : 3 };
console.log(Object.assign(obj1, obj2, obj3))//{ a : 1, b : 2, c:3 }
Copy the code
Extended operator
let obj1 = { a : 1, b : 2 };
let obj2 = { a : 2, c : 3, d : 4 };
letnewObj = { ... obj1,... obj2 }; console.log(newObj) //{a: 2, b: 2, c: 3, d: 4}Copy the code
Tiling multidimensional arrays
var arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] var arr2 = [1, 2, [3, 4, [5, 6]]]; arr2.flat(); // [1, 2, 3, 4, [5, 6]] var arr3 = [1, 2, [3, 4, [5, 6]]]; arr3.flat(2); // [1, 2, 3, 4, 5, 6] // Use Infinity as the depth to expand the nested array arr3. Flat (Infinity); // [1, 2, 3, 4, 5, 6]Copy the code
The flat() method removes empty items from the array:
var arr4 = [1, 2, , 4, 5];
arr4.flat();
// [1, 2, 4, 5]
Copy the code
Boolean Filters all false values in the array
let arr = [ 1 , null , false , 2 , 3 , NaN , ' '] the console. The log (a.f ilter (Boolean)) / / [1, 2, 3]Copy the code
8, integer
For a digital | 0 can be integer, negative number also applies, num | 0
1.3 | 0 // 1
-1.9 | 0 // -1
Copy the code
9, | | and use &&
/ / scenario 1function b(a) {
if (a) {
return a
} else {
return ' '} // Scenario 2function b(a) {
return a || ' '
}
Copy the code
The use of the above is | |, also called short circuit processing. This is common in if conditions, but can also be used directly in statements. If the first argument is true, the value of the first argument is taken; if the first argument is not true, the value of the second argument is taken. && coincided with | | instead. If the first argument is true, it takes the value of the second argument. It is common to use && instead of if-else, for example:
if(a >=5){   alert("Hello");
}
//a >= 5 && alert("Hello");
Copy the code
Use objects instead of switch/if
Public content:let a = 'VIP'Scenario 1if (a === 'VIP') {
return1}else if (a === 'SVIP') {
return2} Scenario 2 Switch (a) {case 'VIP'
return 1
break
case 'SVIP'
return 2
break} scenario 3let obj = {
VIP: 1,
SVIP: 2
}
Copy the code
Available this method maps the state meaning in Chinese, for example, pay state usually get is 1, 2 and 4 of this value, but the list requires display the corresponding Chinese state Did not pay | | in the payment has been refund, etc
Filter object properties with destructively assigned values
Const {inner, outer,... restProps } = { inner:'This is inner',
outer: 'This is outer',
v1: '1',
v2: '2',
v4: '3'
};
console.log(restProps);
// {v1: "1", v2: "2", v4: "3"}
Copy the code
12. Digital amount is formatted in thousands of digits
let num = 123455678;
letNum1 = 123455678.12345;let formatNum = num.toLocaleString('en-US');
let formatNum1 = num1.toLocaleString('en-US'); console.log(formatNum); / / 123455678 console log (formatNum1); / / 123455678123Copy the code