From 69ec3c3fe72dfb79a7241e68d0f926ed3690c6a2 Mon Sep 17 00:00:00 2001 From: myapos Date: Fri, 22 Dec 2017 09:37:36 +0200 Subject: [PATCH] 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` +