In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the JS skills that are helpful to the development". In the daily operation, I believe that many people have doubts about the JS skills that are helpful to the development. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the JS skills that are helpful to the development?" Next, please follow the editor to study!
1. Generate a list with random numbers
Array.from ({length: 1000}, Math.random) / / [0.6163093133259432,0.8877401276499153,0.4094354756035987,...]-1000 items
two。 Generate a list with numbers
Array.from ({length: 1000}, (v, I) = > I) / / [0,1,2,3,4,5,6,999]
3. Convert RGB → to hexadecimal
Const rgb2hex = ([r, g, b]) = > `# ${(1 {try {new URL (url); return true;} catch (error) {return false;}} isValidURL ('https://segmentfault.com/u/minnanitkong/articles') / / true isValidURL ("https//invalidto"); / / false
7. The distance between the past and the present
Sometimes we need to print a date from 6 minutes ago, but we don't want a large library to do it. Here's a little clip that can do this:
Const fromAgo = (date) = > {const ms = Date.now ()-date.getTime (); const seconds = Math.round (ms / 1000); const minutes = Math.round (ms / 60000); const hours = Math.round (ms / 3600000); const days = Math.round (ms / 86400000); const months = Math.round (ms / 2592000000); const years = Math.round (ms / 31104 000000); switch (true) {case seconds
< 60: return `${seconds} second(s) ago"`; case minutes < 60: return `${minutes} minute(s) ago"`; case hours < 24: return `${hours} hour(s) ago"`; case days < 30: return `${days} day(s) ago`; case months < 12: return `${months} month(s) ago`; default: return `${years} year(s) ago`; } }; const createdAt = new Date(2021, 0, 5); fromAgo(createdAt); // 14 day(s) ago; 8. 用参数生成路径 我们在处理路线/路径时常做很多工作,我们总是需要对其进行操作。当我们需要生成带有参数的路径以将浏览器推送到那里时,generatePath 可以帮助我们! const generatePath = (path, obj) =>Path.replace (/ (: [amurz] +) / g, (v) = > obj [v.substr (1)]); const route = "/ app/:page/:id"; generatePath (route, {page: "products", id: 85,}); / / app/products/123
9. Get parameters from the path
Const getPathParams = (path, pathMap, serializer) = > {path = path.split ("/"); pathMap = pathMap.split ("/"); return pathMap.reduce ((acc, crr, I) = > {if (crr [0] = = ":") {const param = crr.substr (1); acc [param] = serializer & serializer [param]? Serializer [param] (path [I]): path [I];} return acc;}, {});}; getPathParams ("/ app/products/123", "/ app/:page/:id") / / {page: 'products', id:' 123'} getPathParams ("/ items/2/id/8583212", "/ items/:category/id/:id", {category: v = > ['Car',' Mobile', 'Home'] [v], id: v = > + v}); / / {category:' Home', id: 8583212}
10. Generate paths with query strings
Const getQueryParams = url = > url.match (/ ([^? = &] +) (= ([^ &] *)) / g). Reduce ((total, crr) = > {const [key, value] = crr.split ("="); total [key] = value; return total;}, {}); getQueryParams ("/ user?name=Orkhan&age=30") / / {name: 'Orkhan', age:' 30'} at this point, the study of "what are the JS skills that are helpful to development" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.