当前位置:网站首页>20 not to be missed ES6 tips

20 not to be missed ES6 tips

2022-06-24 07:19:00 zz_ jesse

Preface

Hello everyone , I am a xieyezi, Long time no see , I'm back in the Nuggets again , This time, I sorted out 20 A high frequency of use ES6 Code block , Hope you enjoy it

The detailed comments and specific usage of the code are in my github On , The source code is connected at the bottom

1. Disorder array order

79bc193d76281bc219f93bed8b55b4d3.png
code1.png

2. Remove all characters except numbers

a719e90f21c6ff63e3634b4b6e369250.png
code20.png

3. Invert a string or word

9ffc7309d8079b5e936aa1c97caf6d02.png
code2.png

4. Convert decimal to binary or hexadecimal

5bc1b1a68b66db2e14a40cc7946a4f49.png
code3.png

5. Merge multiple objects

e5b20e84ab000b6744761453fb3903b0.png
code4.png

6. === and == The difference between

e899ad1ae3d29becb23f875e4a2e2f58.png
code5.png

7. Deconstruct assignment

5e01a7867526f8b50c0a30923175ea72.png
code6.png

8. Exchange the values of variables

db09ffbc6ebef4194bd581ccb6d9b72e.png
code7.png

9-1. Judge palindrome string

b31e84e08d907d3de199fd8ccf916509.png
code21.png

Palindrome string : Write the same string forward and reverse ( Special thanks \@ Floating garret master [1] Corrigendum )

9-2 Judge whether the two strings are arranged with each other

297c0d86e3b123faa8d14446ed1e9939.png
code8.png

Judge whether the two strings are arranged with each other : Given two strings , Whether one is the arrangement of another

10. Optional chain operator

57b53d43340ce45e5c2a46d94c709bce.png
code9.png

MDN: Optional chain The operator ( ?. ) Allows you to read the value of an attribute deep in the chain of connected objects , Instead of explicitly verifying that each reference in the chain is valid .?.  The function of the operator is similar to  .  Chain operators , The difference is , The reference is empty (nullish ) (null  perhaps  undefined) It doesn't cause errors , The short-circuit return value of this expression is  undefined. When used with function calls , If the given function does not exist , Then return to  undefined

for example :

if (res && res.data && res.data.success) {   
   //code
} 
 Copy code 

amount to :

if (res?.data?.success) {
  // code
}
 Copy code 

11. Ternary operator

e426f8eb61b450e6dbccf8227eb05fbd.png
code10.png

12. Randomly select a value from the array

0c85cc7016f99e72fa0babbb22494963.png
code11.png

13. Freeze objects

ffaf5643e242702d0ded523b02347aa3.png
code12.png

14. Delete duplicate elements of the array

465ad29192a28fac3823efd37f78d30f.png
code13.png

15. Retention refers to the positioning of decimals

49b8294a7d16a41216495114718df993.png
code14.png

16. Empty array

2b0ae3c7e8420b4276ef2db072dd8dff.png
code15.png

17. from RGB Convert to HEX

6c92ff7bfbfdacd2d06ffc4ebdf69624.png
code16.png

18. Get the maximum and minimum values from the array

024232a1b031e7ace381a4cec96bf0c3.png
code17.png

19. Null merge operator

26e35ed603b3f24ba5eca2a28ec41c2d.png
code18.png

MDN: Null merge operator ??) It's a logical operator , When the operands on the left are  null  perhaps  undefined  when , Returns its right-hand operand , Otherwise, return the left-hand operand .

20. The value in the filter array is false Value

d08b1db60efd000d98cc4212398b386e.png
code19.png

Source code

20 A must not miss ES6 skill [2]

above , Codeword mapping is hard , Please don't be stingy with your praise , Your praise is the biggest driving force for me to continue to update !

About this article

author :Xieyezi

https://juejin.cn/post/7083145771461115941

原网站

版权声明
本文为[zz_ jesse]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240128152790.html