In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to get started with JavaScript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to get started with JavaScript.
ArrayGcd
Calculates the greatest common denominator (gcd) of an array of numbers.
Use Array.reduce () and the gcd formula (uses recursion) to calculate the greatest common denominator of an array of numbers.
Const arrayGcd = arr = > {const gcd = (x, y) = >! y? X: gcd (y, x% y); return arr.reduce ((gcd) = > gcd);} / / arrayGcd ([1min2, 3d4])-> 1 / / arrayGcd ([4mem8 / 12])-> 4
Calculates the * common divisor of the array.
Use Array.reduce () and gcd formula (using recursion) to calculate the * common divisor of an array.
➜code cat arrayGcd.js const arrayGcd = arr = > {const gcd = (x, y) = >! y? X: gcd (y, x% y); return arr.reduce ((a, b) = > gcd (a, b));} console.log (arrayGcd ([1,2,3,4,5])); console.log (arrayGcd ([4,8,12])); ➜code node arrayGcd.js 1 4
Gcd is the Euclidean algorithm, the specific table, self-check. The reduce method of the array is used here, which is quite concise. If you don't know much about reduce, you can understand it by looking at mdn.
ArrayLcm
Calculates the lowest common multiple (lcm) of an array of numbers.
Use Array.reduce () and the lcm formula (uses recursion) to calculate the lowest common multiple of an array of numbers.
Const arrayLcm = arr = > {const gcd = (x, y) = >! y? X: gcd (y, x% y); const lcm = (x, y) = > (x, y) / gcd (x, y) return arr.reduce ((lcm) = > lcm);} / / arrayLcm ([1m 2m 3J 4J 5])-> 60 / / arrayLcm ([4m 8J 12])-> 24
Calculates the least common multiple of an array.
Use Array.reduce () and lcm formula (using recursion) to calculate the * common divisor of an array.
➜code cat arrayLcm.js const arrayLcm = arr = > {const gcd = (x, y) = > (! y? X: gcd (y, x% y); const lcm = (x, y) = > x * y / gcd (x, y); return arr.reduce ((a, b) = > lcm (a, b));}; console.log (arrayLcm ([1,2,3,4,5])); console.log (arrayLcm ([4,8,12])); ➜code node arrayLcm.js 60 24
The lcm algorithm uses the previous gcd algorithm, and the key point is that the product of the * common divisor and the least common multiple of two numbers is exactly the product of the two numbers.
ArrayMax
Returns the maximum value in an array.
Use Math.max () combined with the spread operator (...) To get the maximum value in the array.
Const arrayMax = arr = > Math.max (.arr); / / arrayMax ([10,1,5])-> 10
Returns the value of * in the array.
Use the extension operators of Math.max () and ES6... Returns the value of * in the array.
➜code cat arrayMax.js const arrayMax = arr = > Math.max (.arr); console.log (arrayMax ([10,1,5]));➜ code node arrayMax.js 10
It's actually what Math.max () did, and there's nothing more to say.
ArrayMin
Returns the minimum value in an array.
Use Math.min () combined with the spread operator (...) To get the minimum value in the array.
Const arrayMin = arr = > Math.min (.arr); / / arrayMin ([10,1,5])-> 1
Returns the smallest value in the array.
Use the extension operators of Math.min () and ES6... Returns the smallest value in the array.
➜code cat arrayMin.js const arrayMin = arr = > Math.min (.arr); console.log (arrayMin ([10,1,5]));➜ code node arrayMin.js 1
It's actually what Math.min () did, and there's nothing more to say.
Chunk
Chunks an array into smaller arrays of a specified size.
Use Array.from () to create a new array, that fits the number of chunks that will be produced. Use Array.slice () to map each element of the new array to a chunk the length of size. If the original array can't be split evenly, the final chunk will contain the remaining elements.
Const chunk = (arr, size) = > Array.from ({length: Math.ceil (arr.length / size)}, (v, I) = > arr.slice (I * size, I * size + size)); / / chunk
Splits an array into smaller array blocks containing the number of size according to a given size.
Use Array.from () to produce new arrays that match the definition. Use Array.slice () to intercept the specified size elements to form a new array block. If the original array length is not divisible by size, the remaining elements of * * will belong to * a block.
➜code cat chunk.js const chunk = (arr, size) = > Array.from ({length: Math.ceil (arr.length / size)}, (v, I) = > arr.slice (I * size, I * size + size)); console.log (chunk ([1,2,3,4,5], 2));➜ code node chunk.js [1,2], [3,4], [5]]
Array.from (arrayLike, mapFn, thisArg) this method, * parameters are an array of classes or iterated objects, the second parameter is a method applied to each array element, and the third parameter is to change the direction of the this. In popular terms, it means to specify who your father is.
Here we use a {length: Math.ceil (arr.length / size)} iterative object, and length specifies the number of iterations, that is, the length of the array after the size is divided into blocks, which is exactly the value of the original array length divided by size. The purpose of rounding up is to satisfy the situation that it is not completely divisible. For example, five elements are divided into two groups, divided into two groups of two elements, leaving one element as an independent group with a total length of 3.
(v, I), because the array is initialized with undefined at every position during iteration, v is always undefined.
During the iteration of arr.slice (I * size, I * size + size), the elements of the number of size are intercepted each time to form a new array. The I here changes with the iteration, for example, length is 3, and I is 0pr 1pr 2.
The iteration here is similar to range in python.
➜code python Python 3.6.4 (default, Dec 23 2017, 10:37:40) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. > import math > arr = [1 math.ceil (len (arr) / size)] > size = 2 > for i in range (len (arr) / size): Print ('index:', I)... Index: 0 index: 1 index: 2
Compact
Removes falsey values from an array.
Use Array.filter () to filter out falsey values (false, null, 0, ", undefined, and NaN).
Const compact = arr = > arr.filter (Boolean); / / compact ([0,1, false, 2,', 3, 'asides,' estrangement 23, NaN, 'slots, 34])-> [1,2,3]
Remove the element of falsey from the array. This falsey is not easy to translate, not the wrong meaning, but the value Boolean operation value is the meaning of false, I personally often use! To make a judgment.
Use Array.filter () to filter out falsey such as false, null, 0, ", undefined, and NaN.
➜code cat compact.js const compact = arr = > arr.filter (Boolean); console.log (compact ([0,1, false, 2, ", 3," a "," e "* 23, NaN," s ", 34]);➜ code node compact.js [1, 2, 3,'a','s', 34])
Array.prototype.filter () did it. There's nothing to say.
CountOccurrences
Counts the occurrences of a value in an array.
Use Array.reduce () to increment a counter each time you encounter the specific value inside the array.
Const countOccurrences = (arr, value) = > arr.reduce ((a, v) = > v = = value? A + 1: a + 0,0); / / countOccurrences ([1 recorder 1 recital 2 recital 2 recorder 3], 1)-> 3
Count the number of times an element appears in an array.
Use Array.reduce () to increase the second value of the specified element if it appears in the array during traversal, and the default number is 0.
➜code cat countOccurrences.js const countOccurrences = (arr, value) = > arr.reduce ((a, v) = > (v = value? A + 1: a + 0), 0); console.log (countOccurrences ([1,1,2,1,2,3], 1)); console.log (countOccurrences ([1,1,2,1,2,3], 5)); ➜code node countOccurrences.js 3 0
Ternary operator (v = value? Determine whether the value v of the traversal array is strictly equal to the specified value value during traversal. Yes, the number of times is 1; no, 0.
The 0 after a comma of * * is the initial value, that is, axi0.This is known to the reduce method, especially because this function must have a return value. if the specified element does not appear once in the array, the return value is 0, so it must be initialized to 0.
DeepFlatten
Deep flattens an array.
Use recursion. Use Array.concat () with an empty array ([]) and the spread operator (...) To flatten an array. Recursively flatten each element that is an array.
Const deepFlatten = arr = > [] .concat (... arr.map (v = > Array.isArray (v)? DeepFlatten (v): v); / / deepFlatten ([1, [2], [[3], 4], 5])-> [1, 2, 3, 4, 5]
Deeply flatten an array.
Use recursive methods. The extension operator that combines Array.concat (), empty array [], and ES6. To flatten out an array, and if the flattened element is still an array, use this method recursively.
➜code cat deepFlatten.js const deepFlatten = arr = > [] .concat (... arr.map (v = > (Array.isArray (v)? DeepFlatten (v): v)); console.log (deepFlatten ([1, [2], [[3], 4], 5])); ➜code node deepFlatten.js [1,2,3,4,5]
Ternary operator (Array.isArray (v)? DeepFlatten (v): v) determines whether v is an array, yes, returns the value after recursively applying deepFlatten (v); no, returns v directly.
[] .concat (... arr.map (fn)) uses an empty array to perform the array produced by the map operation. The extended operation values are spliced and returned into an array of results.
This method is a deep leveling method, and in many cases there is a specific need for leveling a layer, such as underscore. The way to achieve this is to add another flag parameter for processing. Forget the details.
Thank you for your reading, the above is the content of "how to get started with JavaScript". After the study of this article, I believe you have a deeper understanding of how to get started with JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.