prefixed Array. to reduce(), sort(), map() methods in snippets.

This commit is contained in:
piyuesh
2017-12-13 17:41:45 +05:30
parent e9c534c395
commit b995e1bea1
11 changed files with 11 additions and 11 deletions

View File

@ -1,6 +1,6 @@
### URL parameters ### 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 (`...`). 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`. Pass `location.search` as the argument to apply to the current `url`.

View File

@ -2,7 +2,7 @@
Use recursion. Use recursion.
For each letter in the given string, create all the partial anagrams for the rest of its letters. 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`. Base cases are for string `length` equal to `2` or `1`.
```js ```js

View File

@ -1,6 +1,6 @@
### Average of array of numbers ### 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 ```js
const average = arr => const average = arr =>

View File

@ -1,6 +1,6 @@
### Count occurrences of a value in array ### 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 ```js
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0); const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);

View File

@ -1,7 +1,7 @@
### Deep flatten array ### Deep flatten array
Use recursion. 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 ```js
const deepFlatten = arr => const deepFlatten = arr =>

View File

@ -1,6 +1,6 @@
### Flatten array ### 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 ```js
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []); const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);

View File

@ -1,6 +1,6 @@
### Initialize array with range ### 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`. You can omit `start` to use a default value of `0`.
```js ```js

View File

@ -1,6 +1,6 @@
### Powerset ### 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 ```js
const powerset = arr => const powerset = arr =>

View File

@ -1,6 +1,6 @@
### Randomize order of array ### 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 ```js
const randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1); const randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1);

View File

@ -1,6 +1,6 @@
### Sort characters in string (alphabetical) ### 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 ```js
const sortCharactersInString = str => const sortCharactersInString = str =>

View File

@ -1,6 +1,6 @@
### Sum of array of numbers ### 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 ```js
const sum = arr => arr.reduce( (acc , val) => acc + val, 0); const sum = arr => arr.reduce( (acc , val) => acc + val, 0);