From 5a19b255cf4d1faf8035de19c3dd6dbf05bfa672 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 d73724cba4b6179d88408bcadc9742bc9595c04a 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 064325ef18e2bf73f6ff8e9cc4e8ac1c91f68215 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 fd65cbcdd1c5bd901658180285590f3091bb953e 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 0a18ab09579cd1f64154b95ab5655f4bce23e815 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 0d584cd2e0ae7bef8cda3ce0bc0c3ac6968a2b9b 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 7c1641aeb4b55316620344c0ae4b08879585e979 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 1af57a3b61652cb978b7107a092623f959fea8b5 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 3c13644db21bcb411f2e9d664c108dd50eeb76b8 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 b988185275debc09bdc733bd869bdc6d3cad7b78 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 c1716cbc2aea497178b1d7f2fac86d254f6f2aee 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 63d04639f011fd65b9931eb0a325402c0b15639f 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) =>