Skip to content

Commit

Permalink
Merge pull request #512 from Chalarangelo/partition
Browse files Browse the repository at this point in the history
[FEATURE] Add partition
  • Loading branch information
Chalarangelo committed Jan 8, 2018
2 parents ffd30cf + 9a0727a commit 4fb1f9b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions snippets/partition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### 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;},[[],[]]);
```

```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 }]]
```
1 change: 1 addition & 0 deletions tag_database
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ once:function
onUserInputChange:browser,event,advanced
orderBy:object,array
palindrome:string
partition:array,object,function
percentile:math
pick:array
pipeFunctions:adapter,function
Expand Down

0 comments on commit 4fb1f9b

Please sign in to comment.