From 69ec3c3fe72dfb79a7241e68d0f926ed3690c6a2 Mon Sep 17 00:00:00 2001 From: myapos Date: Fri, 22 Dec 2017 09:37:36 +0200 Subject: [PATCH 01/12] added reducedFilter snippet --- snippets/reducedFilter.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 snippets/reducedFilter.md diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md new file mode 100644 index 000000000..8510bbfe0 --- /dev/null +++ b/snippets/reducedFilter.md @@ -0,0 +1,37 @@ +### reducedFilter + +Filter an array of objects based on condition and return array with reduced objects. + +// ----------------------------------- Input -----------------------------------// +// 1. Data: the data to be filtered (array of objects) -------------------------// +// 2. Condition: will be used for filtering (string) ---------------------------// +// 3. outputProps: an array of properties that will be used to contruct --------// +// new array of objects --------------------------------------------------------// + +// ----------------------------------- Output ----------------------------------// +// Filtered array with new objects. Properties of new objects are a subset of --// +// properties of original objects ----------------------------------------------// + +// ----------------------------------- Info ------------------------------------// +// Used ES6 reduce -------------------------------------------------------------// +// Dummy data for testing ------------------------------------------------------// +// Generated with http://www.mockaroo.com/ -------------------------------------// + + +```js +const reducedFilter = (data, condition, outputProps) => + data.reduce( (acc, item) => { + if(eval(condition)) { + const parsedObj = outputProps.reduce( (aggr, index) => { + aggr[index] = item[`${index}`]; + return aggr; + }, {}); + acc.push(parsedObj); + } + return (acc); + }, []); +``` +###Usage Example available in : + +`https://codepen.io/myapos/pen/dJGByW?editors=0112` + From dde5981f11acfe4dbfbf3a1cfb46b46e1e48ee3b Mon Sep 17 00:00:00 2001 From: Apostolakis Myron Date: Fri, 22 Dec 2017 10:29:48 +0200 Subject: [PATCH 02/12] github markup --- snippets/reducedFilter.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index 8510bbfe0..853940c1a 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -2,21 +2,22 @@ Filter an array of objects based on condition and return array with reduced objects. -// ----------------------------------- Input -----------------------------------// -// 1. Data: the data to be filtered (array of objects) -------------------------// -// 2. Condition: will be used for filtering (string) ---------------------------// -// 3. outputProps: an array of properties that will be used to contruct --------// -// new array of objects --------------------------------------------------------// +#### Input -// ----------------------------------- Output ----------------------------------// -// Filtered array with new objects. Properties of new objects are a subset of --// -// properties of original objects ----------------------------------------------// +i. Data: the data to be filtered (array of objects) +ii. Condition: will be used for filtering (string) +iii. outputProps: an array of properties that will be used to contruct new array of objects -// ----------------------------------- Info ------------------------------------// -// Used ES6 reduce -------------------------------------------------------------// -// Dummy data for testing ------------------------------------------------------// -// Generated with http://www.mockaroo.com/ -------------------------------------// +#### Output +Filtered array with new objects. Properties of new objects are a subset of +properties of original objects + +#### Info + +Used ES6 reduce +Dummy data for testing +Generated with http://www.mockaroo.com/ ```js const reducedFilter = (data, condition, outputProps) => From 55be06b5223efed8c48fa2a5a673a3bd43aafe4d Mon Sep 17 00:00:00 2001 From: Apostolakis Myron Date: Fri, 22 Dec 2017 10:37:03 +0200 Subject: [PATCH 03/12] unorderd --- snippets/reducedFilter.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index 853940c1a..754bddece 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -4,9 +4,9 @@ Filter an array of objects based on condition and return array with reduced obje #### Input -i. Data: the data to be filtered (array of objects) -ii. Condition: will be used for filtering (string) -iii. outputProps: an array of properties that will be used to contruct new array of objects +* Data: the data to be filtered (array of objects) +* Condition: will be used for filtering (string) +* outputProps: an array of properties that will be used to contruct new array of objects #### Output From 7ee671490fe31ce5c98ed31e4558e367d7c27725 Mon Sep 17 00:00:00 2001 From: Apostolakis Myron Date: Fri, 22 Dec 2017 10:37:41 +0200 Subject: [PATCH 04/12] minor --- snippets/reducedFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index 754bddece..01b383abe 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -10,7 +10,7 @@ Filter an array of objects based on condition and return array with reduced obje #### Output -Filtered array with new objects. Properties of new objects are a subset of +* Filtered array with new objects. Properties of new objects are a subset of properties of original objects #### Info From 7a0d268f41d9f5bbb85fe4f7e851578470db5860 Mon Sep 17 00:00:00 2001 From: Apostolakis Myron Date: Fri, 22 Dec 2017 10:38:02 +0200 Subject: [PATCH 05/12] minor url --- snippets/reducedFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index 01b383abe..eb95606ac 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -32,7 +32,7 @@ const reducedFilter = (data, condition, outputProps) => return (acc); }, []); ``` -###Usage Example available in : +#### Usage Example available in : `https://codepen.io/myapos/pen/dJGByW?editors=0112` From cf1eabef94c443b4ae6b4b245cb683b97adf584c Mon Sep 17 00:00:00 2001 From: Apostolakis Myron Date: Fri, 22 Dec 2017 10:39:12 +0200 Subject: [PATCH 06/12] ordered --- snippets/reducedFilter.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index eb95606ac..311851407 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -4,9 +4,9 @@ Filter an array of objects based on condition and return array with reduced obje #### Input -* Data: the data to be filtered (array of objects) -* Condition: will be used for filtering (string) -* outputProps: an array of properties that will be used to contruct new array of objects +1. Data: the data to be filtered (array of objects) +1. Condition: will be used for filtering (string) +1. outputProps: an array of properties that will be used to contruct new array of objects #### Output From 439c5fa6c64ab1d10119096bb56219798899b12b Mon Sep 17 00:00:00 2001 From: Apostolakis Myron Date: Fri, 22 Dec 2017 10:39:43 +0200 Subject: [PATCH 07/12] unordered --- snippets/reducedFilter.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index 311851407..eb95606ac 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -4,9 +4,9 @@ Filter an array of objects based on condition and return array with reduced obje #### Input -1. Data: the data to be filtered (array of objects) -1. Condition: will be used for filtering (string) -1. outputProps: an array of properties that will be used to contruct new array of objects +* Data: the data to be filtered (array of objects) +* Condition: will be used for filtering (string) +* outputProps: an array of properties that will be used to contruct new array of objects #### Output From d987d6f4d565dfa8b799d7b004cb3172ce1bde2f Mon Sep 17 00:00:00 2001 From: myapos Date: Sat, 23 Dec 2017 16:57:49 +0200 Subject: [PATCH 08/12] Explained with more details methods and techniques that are used, usage example with inline input and output data, modified snippet code to be compliant with guidelines --- snippets/reducedFilter.md | 56 +++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index eb95606ac..b8b95a195 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -15,24 +15,64 @@ properties of original objects #### Info -Used ES6 reduce -Dummy data for testing -Generated with http://www.mockaroo.com/ +Used ES6 Array.reduce() in order to filter an array with objects based on condition. +If condition is met, then Array.reduce is used again to construct object with the predefined properties. Further more, in order to evaluate condition based in object properties item, must be passed through window to Function constructor. Finally, when new object is constructed then it is saved in the output array. ```js +const safeEval = (condition) => new Function('return '+condition)(); + const reducedFilter = (data, condition, outputProps) => data.reduce( (acc, item) => { - if(eval(condition)) { + window.item = item; + if(safeEval(condition)) { const parsedObj = outputProps.reduce( (aggr, index) => { aggr[index] = item[`${index}`]; return aggr; }, {}); - acc.push(parsedObj); + acc[acc.length]=parsedObj; } return (acc); }, []); + /* + Usage example: + + Input data sample is an array of Objects + + const data = [{ + "id": 1, + "first_name": "Jo", + "last_name": "Blackstone", + "email": "jblackstone0@yahoo.co.jp", + "gender": "Male", + "ip_address": "255.171.18.115" +}, ... ] + +const condition = `window.item.first_name[0] ==='B'`; + +const outputProps = ['first_name','id', 'last_name', 'ip_address']; + +const output = reducedFilter(data, condition, outputProps); --> + +Output conditionally filtered data sample. Properties of output objects are subset of +the properties of the original objects. + +Filtered output data : + +[ +Object { + first_name: "Bertina", + id: 6, + ip_address: "33.239.93.222", + last_name: "Pinching" +}, +Object { + first_name: "Beckie", + id: 13, + ip_address: "239.111.225.202", + last_name: "Thomesson" +}, ... ] + + + */ ``` -#### Usage Example available in : - -`https://codepen.io/myapos/pen/dJGByW?editors=0112` From 4d60d62c88642343b2fb15467e4aee9ca7eaf59e Mon Sep 17 00:00:00 2001 From: myapos Date: Sat, 23 Dec 2017 17:00:05 +0200 Subject: [PATCH 09/12] mend --- snippets/reducedFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index b8b95a195..a880c72bd 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -16,7 +16,7 @@ properties of original objects #### Info Used ES6 Array.reduce() in order to filter an array with objects based on condition. -If condition is met, then Array.reduce is used again to construct object with the predefined properties. Further more, in order to evaluate condition based in object properties item, must be passed through window to Function constructor. Finally, when new object is constructed then it is saved in the output array. +If condition is met, then Array.reduce is used again to construct object with the predefined properties. Further more, in order to dynamically evaluate condition based in object properties, item must be passed through window to Function constructor. Finally, when new object is constructed then it is saved in the output array. ```js const safeEval = (condition) => new Function('return '+condition)(); From c5b456e0978ed795926cd410307196f1e4c08376 Mon Sep 17 00:00:00 2001 From: myapos Date: Sat, 23 Dec 2017 17:00:05 +0200 Subject: [PATCH 10/12] minor changes in info description --- snippets/reducedFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index b8b95a195..a880c72bd 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -16,7 +16,7 @@ properties of original objects #### Info Used ES6 Array.reduce() in order to filter an array with objects based on condition. -If condition is met, then Array.reduce is used again to construct object with the predefined properties. Further more, in order to evaluate condition based in object properties item, must be passed through window to Function constructor. Finally, when new object is constructed then it is saved in the output array. +If condition is met, then Array.reduce is used again to construct object with the predefined properties. Further more, in order to dynamically evaluate condition based in object properties, item must be passed through window to Function constructor. Finally, when new object is constructed then it is saved in the output array. ```js const safeEval = (condition) => new Function('return '+condition)(); From 8b397fc019ceedc121efd46c740032c659666bf0 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 02:04:10 +1100 Subject: [PATCH 11/12] Update reducedFilter.md --- snippets/reducedFilter.md | 97 +++++++++++---------------------------- 1 file changed, 26 insertions(+), 71 deletions(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index a880c72bd..ba94f450c 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -1,78 +1,33 @@ ### reducedFilter -Filter an array of objects based on condition and return array with reduced objects. +Filter an array of objects based on a condition while also filtering out unspecified keys. -#### Input - -* Data: the data to be filtered (array of objects) -* Condition: will be used for filtering (string) -* outputProps: an array of properties that will be used to contruct new array of objects - -#### Output - -* Filtered array with new objects. Properties of new objects are a subset of -properties of original objects - -#### Info - -Used ES6 Array.reduce() in order to filter an array with objects based on condition. -If condition is met, then Array.reduce is used again to construct object with the predefined properties. Further more, in order to dynamically evaluate condition based in object properties, item must be passed through window to Function constructor. Finally, when new object is constructed then it is saved in the output array. +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 safeEval = (condition) => new Function('return '+condition)(); - -const reducedFilter = (data, condition, outputProps) => - data.reduce( (acc, item) => { - window.item = item; - if(safeEval(condition)) { - const parsedObj = outputProps.reduce( (aggr, index) => { - aggr[index] = item[`${index}`]; - return aggr; - }, {}); - acc[acc.length]=parsedObj; - } - return (acc); - }, []); - /* - Usage example: - - Input data sample is an array of Objects - - const data = [{ - "id": 1, - "first_name": "Jo", - "last_name": "Blackstone", - "email": "jblackstone0@yahoo.co.jp", - "gender": "Male", - "ip_address": "255.171.18.115" -}, ... ] - -const condition = `window.item.first_name[0] ==='B'`; - -const outputProps = ['first_name','id', 'last_name', 'ip_address']; - -const output = reducedFilter(data, condition, outputProps); --> - -Output conditionally filtered data sample. Properties of output objects are subset of -the properties of the original objects. - -Filtered output data : - -[ -Object { - first_name: "Bertina", - id: 6, - ip_address: "33.239.93.222", - last_name: "Pinching" -}, -Object { - first_name: "Beckie", - id: 13, - ip_address: "239.111.225.202", - last_name: "Thomesson" -}, ... ] - - - */ +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'}] +``` From a1daf2e695ce9a7033ff8b1b85fcf16680d64143 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 17:05:25 +0200 Subject: [PATCH 12/12] Update reducedFilter.md --- snippets/reducedFilter.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/reducedFilter.md b/snippets/reducedFilter.md index ba94f450c..b34d269fa 100644 --- a/snippets/reducedFilter.md +++ b/snippets/reducedFilter.md @@ -2,8 +2,8 @@ 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. +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) =>