-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipleFiltersWithReduce.js
More file actions
50 lines (46 loc) · 1 KB
/
multipleFiltersWithReduce.js
File metadata and controls
50 lines (46 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const products = [
{
formato: '90x90',
cor: 'azul',
material: 'concreto',
unit: 'M2'
},
{
formato: '90x90',
cor: 'azul',
material: 'concreto',
unit: 'M2'
},
{
formato: '90x90',
cor: 'branco',
material: 'concreto',
unit: 'M2'
},
{
formato: '90x90',
cor: 'azul',
material: 'mármore',
unit: 'M2'
},
{
formato: '90x90',
cor: 'azul',
material: 'madeira',
unit: 'M2'
}
]
const filterByFormat = (product, format) => product.formato === format
const filterByColor = (product, color) => product.cor === color
const filterByMaterial = (product, material) => product.material === material
const filters = [
(product) => filterByColor(product, 'azul'),
(product) => filterByFormat(product, '90x90'),
(product) => filterByMaterial(product, 'concreto')
]
console.log(filters)
const prodReduce = filters.reduce(
(productsFiltered, currentFilter) => productsFiltered.filter(currentFilter),
products
)
console.log(prodReduce)