prefixed Array. to reduce(), sort(), map() methods in snippets.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
### URL parameters
|
||||
|
||||
Use `match()` with an appropriate regular expression to get all key-value pairs, `map()` them appropriately.
|
||||
Use `match()` with an appropriate regular expression to get all key-value pairs, `Array.map()` them appropriately.
|
||||
Combine all key-value pairs into a single object using `Object.assign()` and the spread operator (`...`).
|
||||
Pass `location.search` as the argument to apply to the current `url`.
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Use recursion.
|
||||
For each letter in the given string, create all the partial anagrams for the rest of its letters.
|
||||
Use `map()` to combine the letter with each partial anagram, then `reduce()` to combine all anagrams in one array.
|
||||
Use `Array.map()` to combine the letter with each partial anagram, then `Array.reduce()` to combine all anagrams in one array.
|
||||
Base cases are for string `length` equal to `2` or `1`.
|
||||
|
||||
```js
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Average of array of numbers
|
||||
|
||||
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
||||
|
||||
```js
|
||||
const average = arr =>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Count occurrences of a value in array
|
||||
|
||||
Use `reduce()` to increment a counter each time you encounter the specific value inside the array.
|
||||
Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
|
||||
|
||||
```js
|
||||
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
### Deep flatten array
|
||||
|
||||
Use recursion.
|
||||
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
||||
Use `Array.reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
||||
|
||||
```js
|
||||
const deepFlatten = arr =>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Flatten array
|
||||
|
||||
Use `reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||
Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||
|
||||
```js
|
||||
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Initialize array with range
|
||||
|
||||
Use `Array(end-start)` to create an array of the desired length, `map()` to fill with the desired values in a range.
|
||||
Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
|
||||
You can omit `start` to use a default value of `0`.
|
||||
|
||||
```js
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Powerset
|
||||
|
||||
Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations.
|
||||
Use `Array.reduce()` combined with `Array.map()` to iterate over elements and combine into an array containing all combinations.
|
||||
|
||||
```js
|
||||
const powerset = arr =>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Randomize order of array
|
||||
|
||||
Use `sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting.
|
||||
Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting.
|
||||
|
||||
```js
|
||||
const randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1);
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Sort characters in string (alphabetical)
|
||||
|
||||
Split the string using `split('')`, `sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
||||
Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
||||
|
||||
```js
|
||||
const sortCharactersInString = str =>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Sum of array of numbers
|
||||
|
||||
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||
|
||||
```js
|
||||
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
|
||||
|
||||
Reference in New Issue
Block a user