Deep flatten and housekeeping

Changed original flatten to be named deepFlatten, added normal flatten, improved some other snippets.
This commit is contained in:
Angelos Chalaris
2017-12-12 18:12:24 +02:00
parent 721e1f1d79
commit d44e228e14
5 changed files with 32 additions and 16 deletions

View File

@ -16,6 +16,7 @@
* [Count occurrences of a value in array](#count-occurrences-of-a-value-in-array)
* [Current URL](#current-url)
* [Curry](#curry)
* [Deep flatten array](#deep-flatten-array)
* [Difference between arrays](#difference-between-arrays)
* [Distance between two points](#distance-between-two-points)
* [Divisible by number](#divisible-by-number)
@ -115,7 +116,7 @@ const palindrome = str =>
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
```
@ -141,6 +142,17 @@ const curry = f =>
// curry(Math.pow)(2)(10) -> 1024
```
### 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]
```
### Difference between arrays
Use `filter()` to remove values that are part of `values`, determined using `includes()`.
@ -173,8 +185,7 @@ const isDivisible = (dividend, divisor) => dividend % divisor === 0;
Use `replace()` to escape special characters.
```js
const escapeRegExp = s =>
s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// escapeRegExp('(test)') -> \\(test\\)
```
@ -225,13 +236,11 @@ const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
### 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]
```
## Get scroll position

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]
```