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.