Skip to content

Instantly share code, notes, and snippets.

@kpennell
Created August 1, 2019 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpennell/985df935acfaf4edcc97f5c57801e006 to your computer and use it in GitHub Desktop.
Save kpennell/985df935acfaf4edcc97f5c57801e006 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/sabikil
<!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>
"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