Build README

This commit is contained in:
Angelos Chalaris
2017-12-14 17:02:10 +02:00
parent 12ff77cdff
commit aec1f85ee9

View File

@ -35,7 +35,9 @@
* [Even or odd number](#even-or-odd-number) * [Even or odd number](#even-or-odd-number)
* [Factorial](#factorial) * [Factorial](#factorial)
* [Fibonacci array generator](#fibonacci-array-generator) * [Fibonacci array generator](#fibonacci-array-generator)
* [Fill array](#fill-array)
* [Filter out non unique values in an array](#filter-out-non-unique-values-in-an-array) * [Filter out non unique values in an array](#filter-out-non-unique-values-in-an-array)
* [Flatten array up to depth](#flatten-array-up-to-depth)
* [Flatten array](#flatten-array) * [Flatten array](#flatten-array)
* [Get max value from array](#get-max-value-from-array) * [Get max value from array](#get-max-value-from-array)
* [Get min value from array](#get-min-value-from-array) * [Get min value from array](#get-min-value-from-array)
@ -404,6 +406,18 @@ const fibonacci = n =>
// fibonacci(5) -> [0,1,1,2,3] // fibonacci(5) -> [0,1,1,2,3]
``` ```
[⬆ back to top](#table-of-contents)
### Fill array
Use `Array.map()` to map values between `start` (inclusive) and `end` (exclusive) to `value`.
Omit `start` to start at the first element and/or `end` to finish at the last.
```js
const fillArray = (arr, value, start = 0, end = arr.length) =>
arr.map((v,i) => i>=start && i<end ? value : v);
// fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4]
```
[⬆ back to top](#table-of-contents) [⬆ back to top](#table-of-contents)
### Filter out non-unique values in an array ### Filter out non-unique values in an array
@ -414,6 +428,21 @@ const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexO
// filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5] // filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5]
``` ```
[⬆ back to top](#table-of-contents)
### Flatten array up to depth
Use recursion, decrementing `depth` by 1 for each level of depth.
Use `Array.reduce()` and `Array.concat()` to merge elements or arrays.
Base case, for `depth` equal to `1` stops recursion.
Omit the second element, `depth` to flatten only to a depth of `1` (single flatten).
```js
const flattenDepth = (arr, depth = 1) =>
depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth (v, depth-1) : v), [])
: arr.reduce((a,v) => a.concat(v),[]);
// flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5]
```
[⬆ back to top](#table-of-contents) [⬆ back to top](#table-of-contents)
### Flatten array ### Flatten array
@ -488,10 +517,8 @@ Use `Array.reduce()` to create an object, where the keys are produced from the m
```js ```js
const groupBy = (arr, func) => const groupBy = (arr, func) =>
(typeof func === 'function' ? arr.map(func) : arr.map(val => val[func])) arr.map(typeof func === 'function' ? func : val => val[func])
.reduce((acc, val, i) => { .reduce((acc, val, i) => { acc[val] = (acc[val] || []).concat(arr[i]); return acc; }, {});
acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc;
}, {});
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]} // groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']} // groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
``` ```