Merge remote-tracking branch 'origin/master'

This commit is contained in:
Angelos Chalaris
2018-01-24 12:53:23 +02:00
3 changed files with 45 additions and 2 deletions

View File

@ -146,6 +146,7 @@ average(1, 2, 3);
* [`unionWith`](#unionwith) * [`unionWith`](#unionwith)
* [`uniqueElements`](#uniqueelements) * [`uniqueElements`](#uniqueelements)
* [`unzip`](#unzip) * [`unzip`](#unzip)
* [`unzipWith`](#unzipwith-)
* [`without`](#without) * [`without`](#without)
* [`zip`](#zip) * [`zip`](#zip)
* [`zipObject`](#zipobject) * [`zipObject`](#zipobject)
@ -1997,6 +1998,38 @@ unzip([['a', 1, true], ['b', 2]]); //[['a', 'b'], [1, 2], [true]]
<br>[⬆ Back to top](#table-of-contents) <br>[⬆ Back to top](#table-of-contents)
### unzipWith ![advanced](/advanced.svg)
Creates an array of elements, ungrouping the elements in an array produced by [zip](#zip) and applying the provided function.
Use `Math.max.apply()` to get the longest subarray in the array, `Array.map()` to make each element an array.
Use `Array.reduce()` and `Array.forEach()` to map grouped values to individual arrays.
Use `Array.map()` and the spread operator (`...`) to apply `fn` to each individual group of elements.
```js
const unzipWith = (arr, fn) =>
arr
.reduce(
(acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
Array.from({
length: Math.max(...arr.map(x => x.length))
}).map(x => [])
)
.map(val => fn(...val));
```
<details>
<summary>Examples</summary>
```js
unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)); // [3, 30, 300]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### without ### without
Filters out the elements of an array, that have one of the specified values. Filters out the elements of an array, that have one of the specified values.

File diff suppressed because one or more lines are too long

View File

@ -12,7 +12,7 @@ const unzipWith = (arr, fn) =>
.reduce( .reduce(
(acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc), (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
Array.from({ Array.from({
length: Math.max(...arr.map(x => x.length)), length: Math.max(...arr.map(x => x.length))
}).map(x => []) }).map(x => [])
) )
.map(val => fn(...val)); .map(val => fn(...val));