@@ -34,15 +34,25 @@ function generateListings(numberOfListings) {
34
34
return listings ;
35
35
}
36
36
const tableBodyTag = document . getElementById ( "tableBody" )
37
- const listings = generateListings ( 37 ) ;
37
+ const listings = generateListings ( 1000 ) ;
38
38
39
39
// Filter listings
40
40
const filter = {
41
41
type : 'farm' ,
42
+ facilities : [ 'Parkering' , 'Husdyr' ] ,
42
43
minPrice : 1500 ,
43
44
maxPrice : 8000 ,
45
+ hasGarden : false ,
46
+ minSize : 50 ,
47
+ maxSize : 700
44
48
} ;
45
49
50
+ // add filter do DOM with id filter
51
+ const filterTag = document . getElementById ( "filter" )
52
+ // JSON stringify with line breaks and spaces
53
+ filterTag . innerHTML = JSON . stringify ( filter , null , 2 )
54
+
55
+
46
56
// const filterListings = (listings, filter) => {
47
57
// const filteredItems = listings
48
58
// .filter(listing => listing.type.toLowerCase() === filter.type)
@@ -53,13 +63,24 @@ const filter = {
53
63
const filterListings = ( listings , filter ) => {
54
64
const allowedKeys = [ "type" , "facilities" , "minPrice" , "maxPrice" , "hasGarden" , "minSize" , "maxSize" ]
55
65
const filteredKeys = Object . keys ( filter ) . filter ( element => allowedKeys . includes ( element ) )
56
- // console.log(filteredKeys)
66
+ console . log ( filteredKeys )
57
67
58
68
const filterFunctions = {
59
- type : listing => listing . type . toLowerCase ( ) === filter . type ,
69
+ type : listing => listing . type . toLowerCase ( ) === filter . type . toLowerCase ( ) ,
70
+ // next line is a bit more complicated, but it works
71
+ // it checks if the filter.facilities array includes any of the listing.facilities
72
+ // for filter.facilities.every see:
73
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
74
+ facilities : listing => filter . facilities . every ( facility => {
75
+ const listingFacilities = listing . facilities . map ( facility => facility . toLowerCase ( ) )
76
+ const filterFacility = facility . toLowerCase ( )
77
+ return listingFacilities . includes ( filterFacility )
78
+ } ) ,
60
79
minPrice : listing => listing . price >= filter . minPrice ,
61
80
maxPrice : listing => listing . price <= filter . maxPrice ,
62
- // add key and filtering function for each allowedKey from allowedKeys
81
+ hasGarden : listing => listing . hasGarden === filter . hasGarden ,
82
+ minSize : listing => listing . size >= filter . minSize ,
83
+ maxSize : listing => listing . size <= filter . maxSize ,
63
84
}
64
85
65
86
filteredKeys . forEach ( key => {
0 commit comments