Travis build: 1079
This commit is contained in:
32
README.md
32
README.md
@ -115,6 +115,7 @@ average(1, 2, 3);
|
||||
* [`maxN`](#maxn)
|
||||
* [`minN`](#minn)
|
||||
* [`nthElement`](#nthelement)
|
||||
* [`partition`](#partition)
|
||||
* [`pick`](#pick)
|
||||
* [`pull`](#pull)
|
||||
* [`pullAtIndex`](#pullatindex)
|
||||
@ -1197,6 +1198,37 @@ nthElement(['a', 'b', 'b'], -3); // 'a'
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### partition
|
||||
|
||||
Groups the elements into two arrays, depending on the provided function's truthiness for each element.
|
||||
|
||||
Use `Array.reduce()` to create an array of two arrays.
|
||||
Use `Array.push()` to add elements for which `fn` returns `true` to the first array and elements for which `fn` returns `false` to the second one.
|
||||
|
||||
```js
|
||||
const partition = (arr, fn) =>
|
||||
arr.reduce(
|
||||
(acc, val, i, arr) => {
|
||||
acc[fn(val, i, arr) ? 0 : 1].push(val);
|
||||
return acc;
|
||||
},
|
||||
[[], []]
|
||||
);
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
var users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }];
|
||||
partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]]
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### pick
|
||||
|
||||
Picks the key-value pairs corresponding to the given keys from an object.
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -7,13 +7,16 @@ Use `Array.push()` to add elements for which `fn` returns `true` to the first ar
|
||||
|
||||
```js
|
||||
const partition = (arr, fn) =>
|
||||
arr.reduce((acc, val, i, arr) => {acc[fn(val,i,arr) ? 0 :1].push(val); return acc;},[[],[]]);
|
||||
arr.reduce(
|
||||
(acc, val, i, arr) => {
|
||||
acc[fn(val, i, arr) ? 0 : 1].push(val);
|
||||
return acc;
|
||||
},
|
||||
[[], []]
|
||||
);
|
||||
```
|
||||
|
||||
```js
|
||||
var users = [
|
||||
{ 'user': 'barney', 'age': 36, 'active': false },
|
||||
{ 'user': 'fred', 'age': 40, 'active': true }
|
||||
];
|
||||
partition(users, o => o.active) // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]]
|
||||
var users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }];
|
||||
partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user