JavaScript easy to understand introduction __java

Source: Internet
Author: User
Tags compact
Array Arraymax

Returns the maximum value in an array.

Use the Math.max () with the extension operator (...) to get the maximum value in the array.

Const ARRAYMAX = arr => math.max (... arr); Arraymax ([1, 5])-> 10
Arraymin

Returns the smallest value in an array.

Use the Math.min () with the extension operator (...) to get the smallest value in the array.

Const ARRAYMIN = arr => math.min (... arr); Arraymin ([1, 5])-> 1
Chunk

Divides an array block into a smaller array of the specified size.

Use Array.from () to create a new array, which conforms to the number of blocks that will be generated. Use Array.slice () to map each element of the new array to a block of size length. If the original array cannot be split evenly, the final block will contain the remaining elements.

Const CHUNK = (arr, size) => Array.from ({Length:Math.ceil (Arr.length/size)}, (V, i) => arr.slice (i * size, I * si Ze + size)); Chunk ([1,2,3,4,5], 2)-> [[1,2],[3,4],[5]]
Compact

Removes the Falsey value from the 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, ' a ', ' e ' *23, NaN, ' s ', d])-> [1, 2, 3, ' A ', ' s ', 34]
countoccurrences

Calculates the number of occurrences of a value in an array.

Use Array.reduce () to increment the counter each time a specific value in the array is encountered.

Const Countoccurrences = (arr, value) => arr.reduce ((A, v) => v = = value, a + 1:a + 0, 0); Countoccurrences ([1,1,2,1,2,3], 1)-> 3
Deepflatten

A deep flattener array.

Use recursion. Flatten an array using the Array.concat () and the Empty array ([]) and the spread operator (...). Recursive collaboration each element of an array.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.