In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how the nine extremely powerful JavaScript skills are, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The so-called hacker method is a continuous improvement and iterative construction method. Programmers with the spirit of hacker believe that there is always room for improvement and that nothing is perfect. Each piece of code has room for further optimization, and each operation has a more convenient skill.
Here are some very powerful JavaScript hack techniques.
1. Replace All
We know that the string.Replace () function will only replace the first item.
You can replace everything by adding / g to the end of the regular expression.
Var example = "potato potato"; console.log (example.replace (/ pot/, "tom")); / / "tomato potato" console.log (example.replace (/ pot/g, "tom")); / / "tomato tomato"
two。 Extract unique valu
We can use the Set object and the Spread operator to create a new array that removes duplicate values.
Var entries = [1, 2, 2, 3, 4, 5, 6, 6, 7, 7, 8, 4, 2, 1] var unique_entries = [... new Set (entries)]; console.log (unique_entries); / / [1,2,3,4,5,6,7,8]
3. Convert a number to a string
All we need to do is use the concatenation operator with empty quotes.
Var converted_number = 5 + "; console.log (converted_number); / / 5 console.log (typeof converted_number); / / string
4. Convert a string to a number
Use the + operator.
Please note the usage here, because it only applies to "string numbers".
The_string = "123"; console.log (+ the_string); / / 123the_string =" hello "; console.log (+ the_string); / / NaN
5. Randomly arranging elements in an array
I line up at random every day.
Var my_list = [1,2,3,4,5,6,7,8,9]; console.log (my_list.sort (function () {return Math.random ()-0.5})); / / [4,8,2,9,1,3,6,5,7]
6. Flattened multidimensional array
Just use the Spread operator.
Var entries = [1, [2, 5], [6, 7], 9]; var flat_entries = [] .concat (....); / / [1, 2, 5, 6, 7, 9]
7. Short circuit condition
For example:
If (available) {addToCart ();}
You can shorten it simply by using variables and functions:
Available & & addToCart ()
8. Dynamic attribute name
I always thought I had to declare an object before I could assign a dynamic property.
Const dynamic = 'flavour'; var item = {name:' Coke', [dynamic]: 'Cherry'} console.log (item); / / {name: "Coke", flavour: "Cherry"}
9. Resize / empty the array using length
It's basically the length that overrides the array.
If we want to resize the array:
Var entries = [1,2,3,4,5,6,7]; console.log (entries.length); / / 7 entries.length = 4; console.log (entries.length); / / 4 console.log (entries); / / [1,2,3,4]
If we want to empty the array:
Var entries = [1, 2, 3, 4, 5, 6, 7]; console.log (entries.length); / / 7 entries.length = 0; console.log (entries.length); / / 0 console.log (entries); / / [] on how the 9 extremely powerful JavaScript skills are shared here, I hope the above content can help you to learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.