In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the excellent JavaScript code that can be understood in 30 seconds. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Today to share with you a project, which carefully collected a large number of useful JavaScript code fragments, so that you can understand in a very short time to use them, divided into dates, nodes, functional modules and other parts, you can directly import the code of the file into your text editor (VSCode,Atom,Sublime).
This project is very popular on Github, with a current standard of 71.3K and a cumulative branch of 7.9K (Github address: https://github.com/30-seconds/30-seconds-of-code))
Let's take a look at the code snippets in this project:
Array: arrayMax
Returns the maximum value in the array. Combine Math.max () with the extension operator (...). Used together to get the maximum value in the array.
Const arrayMin = arr = > Math.min (.arr); / / arrayMin ([10,1,5])-> 1 browser: bottomVisible
Returns true if the bottom of the page is visible, otherwise false. Use scrollY, scrollHeight, and clientHeight to determine whether the bottom of the page is visible.
Const bottomVisible = () = > document.documentElement.clientHeight + window.scrollY > = document.documentElement.scrollHeight | | document.documentElement.clientHeight; / / bottomVisible ()-> true date: getDaysDiffBetweenDates
Returns the difference between two dates in days. Calculates the difference between Date objects (in days).
Const getDaysDiffBetweenDates = (dateInitial, dateFinal) = > (dateFinal-dateInitial) / (1000 * 3600 * 24); / / getDaysDiffBetweenDates (new Date ("2017-12-13"), new Date ("2017-12-22"))-> 9 function: chainAsync
Chain asynchronous functions, iterate through an array of functions containing asynchronous events, and call next when each asynchronous event is complete.
Const chainAsync = fns = > {let curr = 0; const next = () = > fns [curr++] (next); next ();}; / * chainAsync ([next = > {console.log ('0 seconds'); setTimeout (next, 1000);}, next = > {console.log ('1 second'); setTimeout (next, 1000);}, next = > {console.log ('2 seconds');}]) * / Mathematics: arrayAverage
Returns the average of an array of numbers. Use Array.reduce () to add each value to the accumulator and initialize it with a value of 0 divided by the array's length.
Const arrayAverage = arr = > arr.reduce ((acc, val) = > acc + val, 0) / arr.length; / / arrayAverage ([1m 2m 3])-> 2 nodes: JSONToFile
Writes a JSON object to a file. Write the json object to the .json file using fs.writeFile (), template text, and JSON.stringify ().
Const fs = require ('fs'); const JSONToFile = (obj, filename) = > fs.writeFile (`${filename} .json`, JSON.stringify (obj, null, 2)) / / JSONToFile ({test: "is passed"},' testJsonFile')-> writes the object to 'testJsonFile.json' object: cleanObj
Removes any properties other than those specified from the JSON object. Use the Object.keys () method to iterate through a given json object and delete keys that are not included in a given array. In addition, if you give it a special key (childIndicator), it will dig deep inside and apply the function to the internal object.
Const cleanObj = (obj, keysToKeep = [], childIndicator) = > {Object.keys (obj) .forEach (key = > {if (key = childIndicator) {cleanObj (obj [key], keysToKeep, childIndicator);} else if (! keysToKeep.includes (key)) {delete obj [key] }})} / * const testObj = {a: 1, b: 2, children: {a: 1, b: 2}} cleanObj (testObj, ["a"], "children") console.log (testObj) / / {a: 1, children: {a: 1}}
The above examples are just the tip of the iceberg. Mark up if you are interested in this project.
After reading the above, do you have any further understanding of the excellent JavaScript code that you can understand in 30 seconds? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.