Created
August 1, 2019 12:35
-
-
Save kpennell/985df935acfaf4edcc97f5c57801e006 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/sabikil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
var filter = { | |
cartridges: true, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}; | |
var filtersArray = ["cartridges", "thcacrystalline"]; | |
var locations = [{ | |
name: "spotX", | |
cartridges: true, | |
highcbdtincture: false, | |
thcacrystalline: false, | |
delivery: true | |
}, { | |
name: "spotY", | |
cartridges: true, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}, { | |
name: "spotZ", | |
cartridges: false, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}]; | |
/* | |
The problem is that it return true as soon as it finds one match... | |
I need for this filter to check if all of the location properties are included in the filter | |
I can't just check if just one location property matches | |
I also can't check if the properties are identical to the filters because sometimes one filter will be false and this would disqualify the location | |
*/ | |
// locations = locations.filter(function(location) { | |
// let locationTrueItems = []; | |
// for (let [key, value] of Object.entries(location)) { | |
// if (value === true) { | |
// locationTrueItems.push(key); | |
// } | |
// console.log(locationTrueItems); | |
// var found = locationTrueItems.every(r=> filtersArray.includes(r)) | |
// console.log(found) | |
// } | |
// }); | |
//console.log(locations); | |
// // first filter | |
// function filterRating(hotel) { | |
// return hotel.rating >= filters.rating; | |
// } | |
// // second filter | |
// function filterMeal(hotel) { | |
// return !filters.mealType.length || hotel.mealType == filters.mealType; | |
// } | |
// // apply both filters to initial array | |
// function update() { | |
// let filteredCards = hotels.filter(filterRating).filter(filterMeal); | |
// }; | |
// cartridges: true, | |
// highcbdtincture: false, | |
// thcacrystalline: false, | |
// delivery: true | |
function filterCartridges(location) { | |
return location.cartridges === filter.cartridges; | |
} | |
function filterHighcbdtincture(location) { | |
return location.highcbdtincture === filter.highcbdtincture; | |
} | |
function filterThcacrystalline(location) { | |
return location.thcacrystalline === filter.thcacrystalline; | |
} | |
function filterDelivery(location) { | |
return location.delivery === filter.delivery; | |
} | |
var filteredLocations = locations.filter(filterCartridges).filter(filterHighcbdtincture).filter(filterThcacrystalline).filter(filterDelivery); | |
console.log(filteredLocations); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var filter = { | |
cartridges: true, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}; | |
var filtersArray = ["cartridges", "thcacrystalline"]; | |
var locations = [ | |
{ | |
name: "spotX", | |
cartridges: true, | |
highcbdtincture: false, | |
thcacrystalline: false, | |
delivery: true | |
}, | |
{ | |
name: "spotY", | |
cartridges: true, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}, | |
{ | |
name: "spotZ", | |
cartridges: false, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
} | |
]; | |
/* | |
The problem is that it return true as soon as it finds one match... | |
I need for this filter to check if all of the location properties are included in the filter | |
I can't just check if just one location property matches | |
I also can't check if the properties are identical to the filters because sometimes one filter will be false and this would disqualify the location | |
*/ | |
// locations = locations.filter(function(location) { | |
// let locationTrueItems = []; | |
// for (let [key, value] of Object.entries(location)) { | |
// if (value === true) { | |
// locationTrueItems.push(key); | |
// } | |
// console.log(locationTrueItems); | |
// var found = locationTrueItems.every(r=> filtersArray.includes(r)) | |
// console.log(found) | |
// } | |
// }); | |
//console.log(locations); | |
// // first filter | |
// function filterRating(hotel) { | |
// return hotel.rating >= filters.rating; | |
// } | |
// // second filter | |
// function filterMeal(hotel) { | |
// return !filters.mealType.length || hotel.mealType == filters.mealType; | |
// } | |
// // apply both filters to initial array | |
// function update() { | |
// let filteredCards = hotels.filter(filterRating).filter(filterMeal); | |
// }; | |
// cartridges: true, | |
// highcbdtincture: false, | |
// thcacrystalline: false, | |
// delivery: true | |
function filterCartridges(location){ | |
return location.cartridges === filter.cartridges | |
} | |
function filterHighcbdtincture(location){ | |
return location.highcbdtincture === filter.highcbdtincture | |
} | |
function filterThcacrystalline(location){ | |
return location.thcacrystalline === filter.thcacrystalline | |
} | |
function filterDelivery(location){ | |
return location.delivery === filter.delivery | |
} | |
let filteredLocations = locations | |
.filter(filterCartridges) | |
.filter(filterHighcbdtincture) | |
.filter(filterThcacrystalline) | |
.filter(filterDelivery); | |
console.log(filteredLocations)</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var filter = { | |
cartridges: true, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}; | |
var filtersArray = ["cartridges", "thcacrystalline"]; | |
var locations = [{ | |
name: "spotX", | |
cartridges: true, | |
highcbdtincture: false, | |
thcacrystalline: false, | |
delivery: true | |
}, { | |
name: "spotY", | |
cartridges: true, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}, { | |
name: "spotZ", | |
cartridges: false, | |
highcbdtincture: true, | |
thcacrystalline: true, | |
delivery: false | |
}]; | |
/* | |
The problem is that it return true as soon as it finds one match... | |
I need for this filter to check if all of the location properties are included in the filter | |
I can't just check if just one location property matches | |
I also can't check if the properties are identical to the filters because sometimes one filter will be false and this would disqualify the location | |
*/ | |
// locations = locations.filter(function(location) { | |
// let locationTrueItems = []; | |
// for (let [key, value] of Object.entries(location)) { | |
// if (value === true) { | |
// locationTrueItems.push(key); | |
// } | |
// console.log(locationTrueItems); | |
// var found = locationTrueItems.every(r=> filtersArray.includes(r)) | |
// console.log(found) | |
// } | |
// }); | |
//console.log(locations); | |
// // first filter | |
// function filterRating(hotel) { | |
// return hotel.rating >= filters.rating; | |
// } | |
// // second filter | |
// function filterMeal(hotel) { | |
// return !filters.mealType.length || hotel.mealType == filters.mealType; | |
// } | |
// // apply both filters to initial array | |
// function update() { | |
// let filteredCards = hotels.filter(filterRating).filter(filterMeal); | |
// }; | |
// cartridges: true, | |
// highcbdtincture: false, | |
// thcacrystalline: false, | |
// delivery: true | |
function filterCartridges(location) { | |
return location.cartridges === filter.cartridges; | |
} | |
function filterHighcbdtincture(location) { | |
return location.highcbdtincture === filter.highcbdtincture; | |
} | |
function filterThcacrystalline(location) { | |
return location.thcacrystalline === filter.thcacrystalline; | |
} | |
function filterDelivery(location) { | |
return location.delivery === filter.delivery; | |
} | |
var filteredLocations = locations.filter(filterCartridges).filter(filterHighcbdtincture).filter(filterThcacrystalline).filter(filterDelivery); | |
console.log(filteredLocations); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment