Merge branch 'master' of https://github.com/Chalarangelo/30-seconds-of-code into add-chunkArray

This commit is contained in:
King
2017-12-12 11:32:33 -05:00
11 changed files with 64 additions and 32 deletions

View File

@ -3,6 +3,6 @@
Use `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);
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
// countOccurrences([1,1,2,1,2,3], 1) -> 3
```

View File

@ -0,0 +1,10 @@
### Deep flatten array
Use recursion.
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
```js
const deepFlatten = arr =>
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []);
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
```

View File

@ -3,7 +3,6 @@
Use `replace()` to escape special characters.
```js
const escapeRegExp = s =>
s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// escapeRegExp('(test)') -> \\(test\\)
```

View File

@ -1,10 +1,8 @@
### Flatten array
Use recursion.
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
Use `reduce()` to get all elements inside the array and `concat()` to flatten them.
```js
const flatten = arr =>
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []);
// flatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
// flatten([1,[2],3,4) -> [1,2,3,4]
```

View File

@ -1,4 +1,4 @@
## Get scroll position
### Get scroll position
Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft` and `scrollTop`.
You can omit `el` to use a default value of `window`.

View File

@ -1,9 +1,9 @@
### Initialize array with values
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
You can omit `v` to use a default value of `0`.
You can omit `value` to use a default value of `0`.
```js
const initializeArray = (n, v = 0) => Array(n).fill(v);
const initializeArray = (n, value = 0) => Array(n).fill(value);
// initializeArray(5, 2) -> [2,2,2,2,2]
```

View File

@ -4,8 +4,8 @@ Use `performance.now()` to get start and end time for the function, `console.log
First argument is the function name, subsequent arguments are passed to the function.
```js
const timeTaken = (f,...args) => {
var t0 = performance.now(), r = f(...args);
const timeTaken = (func,...args) => {
var t0 = performance.now(), r = func(...args);
console.log(performance.now() - t0);
return r;
}

View File

@ -3,6 +3,6 @@
Use `Array.reduce()` to create and combine key-value pairs.
```js
const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {});
const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {});
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
```

8
snippets/pipe.md Normal file
View File

@ -0,0 +1,8 @@
### Pipe
Use `Array.reduce()` to pass value through functions.
```js
const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg);
// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA=="
```

View File

@ -3,7 +3,6 @@
Use `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);
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
// sum([1,2,3,4]) -> 10
```