Update reducedFilter.md

This commit is contained in:
atomiks
2018-01-02 02:04:10 +11:00
committed by GitHub
parent 3eb20a2730
commit 8b397fc019

View File

@ -1,78 +1,33 @@
### reducedFilter ### 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 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.
* 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.
```js ```js
const safeEval = (condition) => new Function('return '+condition)(); const reducedFilter = (data, keys, fn) =>
data.filter(fn).map(el =>
const reducedFilter = (data, condition, outputProps) => keys.reduce((acc, key) => {
data.reduce( (acc, item) => { acc[key] = el[key];
window.item = item; return acc;
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"
}, ... ]
*/
``` ```
```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'}]
```