diff --git a/README.md b/README.md index 84a17213f..57a12fcd5 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,15 @@ +### _Uncategorized_ + +
+View contents + +* [`reducedFilter`](#reducedfilter) + +
+ --- ## 🔌 Adapter @@ -4243,6 +4252,7 @@ Use the spread operator (`...`) to check if the provided argument is iterable in + const isArrayLike = val => try {return [...val], true; } catch (e) { return false; } @@ -4703,6 +4713,45 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### reducedFilter + +Filter an array of objects based on a condition while also filtering out unspecified keys. + +Use `Array.filter()` to filter the array based on the predicate `fn` so that it returns the objects for which the condition returned a truthy value. +On the filtered array, use `Array.map()` to return the new object using `Array.reduce()` to filter out the keys which were not supplied as the `keys` argument. + +```js +const reducedFilter = (data, keys, fn) => + data.filter(fn).map(el => + keys.reduce((acc, key) => { + acc[key] = el[key]; + return acc; + }, {}) + ); +``` + +```js +const data = [ + { + id: 1, + name: 'john', + age: 24 + }, + { + id: 2, + name: 'mike', + age: 50 + } +]; + +reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}] +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index 2fb24395e..b72f07002 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 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);
+    }

 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 ]
@@ -896,6 +896,7 @@ isArray([1]); // true
 

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (...) to check if the provided argument is iterable inside a try... catch block and the comma operator (,) to return the appropriate value.


 
 
+
 const isArrayLike = val =>
   try {return [...val], true; }
   catch (e)  { return false; }
@@ -1000,4 +1001,25 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
+

Uncategorized

reducedFilter

Filter an array of objects based on a condition while also filtering out unspecified keys.

Use Array.filter() to filter the array based on the predicate fn so that it returns the objects for which the condition returned a truthy value. On the filtered array, use Array.map() to return the new object using Array.reduce() to filter out the keys which were not supplied as the keys argument.

const reducedFilter = (data, keys, fn) =>
+  data.filter(fn).map(el =>
+    keys.reduce((acc, key) => {
+      acc[key] = el[key];
+      return acc;
+    }, {})
+  );
+
const data = [
+  {
+    id: 1,
+    name: 'john',
+    age: 24
+  },
+  {
+    id: 2,
+    name: 'mike',
+    age: 50
+  }
+];
+
+reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}]
 

\ No newline at end of file diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index f55ad8328..bd9a13e43 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -8,6 +8,7 @@ Use the spread operator (`...`) to check if the provided argument is iterable in + const isArrayLike = val => try {return [...val], true; } catch (e) { return false; } diff --git a/tag_database b/tag_database index 9d64ea1b5..a4a80199d 100644 --- a/tag_database +++ b/tag_database @@ -115,6 +115,7 @@ randomIntegerInRange:math randomNumberInRange:math readFileLines:node redirect:browser +reducedFilter:uncategorized remove:array repeatString:string reverseString:string