diff --git a/README.md b/README.md index 88ee15553..55a0fcc7c 100644 --- a/README.md +++ b/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'
[⬆ 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; + }, + [[], []] + ); +``` + +
+Examples + +```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 }]] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### pick Picks the key-value pairs corresponding to the given keys from an object. diff --git a/docs/index.html b/docs/index.html index 269bb47a2..8aba44eae 100644 --- a/docs/index.html +++ b/docs/index.html @@ -40,7 +40,7 @@ },1700); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -188,6 +188,16 @@ Object.assig
 

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return []. Omit the second argument, n, to get the first element of the array.

const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
 
nthElement(['a', 'b', 'c'], 1); // 'b'
 nthElement(['a', 'b', 'b'], -3); // 'a'
+

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.

const partition = (arr, fn) =>
+  arr.reduce(
+    (acc, val, i, arr) => {
+      acc[fn(val, i, arr) ? 0 : 1].push(val);
+      return acc;
+    },
+    [[], []]
+  );
+
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 }]]
 

pick

Picks the key-value pairs corresponding to the given keys from an object.

Use Array.reduce() to convert the filtered/picked keys back to an object with the corresponding key-value pair if the key exists in the obj.

const pick = (obj, arr) =>
   arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
 
pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 }
diff --git a/snippets/partition.md b/snippets/partition.md
index f347962ee..2f48435d5 100644
--- a/snippets/partition.md
+++ b/snippets/partition.md
@@ -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 }]]
 ```