JS operation tips, the work is simple

1. Add 0 operation

Scenario: page month and day display, need to use 0 or other character placeholder.

var number = 11; 
('0' + number).slice(-2);
Copy the code

2, thousandths division

Scenario: Page display amount, digit division.

Const AmountSlite = (value) => {const regExp = /(\d{1,3})(? =(\d{3})+(? : $| \.) )/g; const ret = (value +' ').replace(regExp, '$1');
    return ret;
};
Copy the code

3. Check the digits from 0 to 100 with a maximum of two valid digits

Scenario: Percentage check, input box digit check

Var/reg = ^ \ d \. ([1-9] {1, 2} | [0-9] [1-9]) $| ^ [0-9] \ d {0, 1} (\. \ d {1, 2}) {0, 1} $| ^ 100 (\. 0} {1, 2) {0, 1} $/; Var reg1 = /^[1-9](\d+)? (\ \ d {1, 2})? $) | (^ \ \ d \ d {1, 2} $/ / / is greater than or equal to 0, retain two decimal places var reg2 = / ^ (0 | \ [1-9] d *) (\ s $| | \ \ d {1, 2} \ b) /Copy the code

4. Swap two numbers quickly

Scenario: Algorithmic sort swaps two values

let x = 1;
let y = 2;
[x ,y] = [y, x];
Copy the code

5. Get the last day of the month

Scenario: depending on the month, or the last day of the month

new Date('2020'.'1', 0).getDate();
Copy the code

6. Use the keyword void

Scenario: Using the void operator ensures that you get a true undefined. Also used for minimization purposes.

console.log(1); // 1
console.log(void 1); 

let obj = {
    m: 1
};
console.log(void obj.m);
Copy the code

7, arbitrary type judgment

Scenario: Get variable types

const type = data => Object.prototype.toString.call(data).replace(/^\[object (.+)\]$/, '$1').toLowerCase();
Copy the code

Js evokes wechat

Scenario: H5 operates wechat

window.location.href="weixin://"; / / H5 operating WeChat: http://www.xiaoshu168.com/jquery/172.htmlCopy the code

9. Filter the falsy value in the array

Scenario: Filter the array for falsy values you don’t want

const arr = [0, 1, '0'.'1', +0, -0, undefined, null, 'undefined'.'null'.' '.'snail'.true.false, NaN, 'NaN'];
arr.filter(Boolean);
Copy the code

Comment on your work tips for everyone to learn.