Created
March 16, 2016 06:02
-
-
Save alexklibisz/dac977a2612888b85f71 to your computer and use it in GitHub Desktop.
quick-and-dirty-multi-property-search-02
This file contains hidden or 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
function matchFilter(allItems, query, threshold) { | |
// Create an array of properties that are defined in the query. | |
// For the example, it will be [food_type, neighborhood] | |
const properties = Object.keys(query) | |
.filter(key => query[key].trim().length > 0); | |
// Create a comparison string for the query item. | |
// For the example, it will be "Mxicanmarketsquare" | |
const queryComp = properties.map(p => query[p]).join(); | |
// Filter down to get the matching items. | |
const matchingItems = allItems.filter((item) => { | |
// Create a comparison string for the current | |
// item that consists of the property values | |
// that are included in the query. | |
const itemComp = properties.map(p => item[p]).join(); | |
// Compare itemComp string to the queryComp. | |
// Statement evaluates to true, then the item matches! | |
return itemComp.score(queryComp) >= threshold; | |
}); | |
return matchingItems; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment