Created
August 2, 2019 12:33
-
-
Save trungpv1601/5cb693cbc31a79251046ca60cd2de8c0 to your computer and use it in GitHub Desktop.
Filter JavaScript Objects with Fuzzy Search
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
| // Thanks to https://bytemaster.io/filter-javascript-objects-fuzzy-search | |
| Array.prototype.fuzzySearch = function (query) { | |
| var search = query.split(' '); | |
| var ret = this.reduce((found, i) => { | |
| var matches = 0; | |
| search.forEach(s => { | |
| var props = 0; | |
| for (var prop in i) { | |
| if (i[prop].indexOf(s) > -1) { | |
| props++; | |
| } | |
| } | |
| if (props >= 1) { | |
| matches++; | |
| } | |
| }) | |
| if (matches == search.length) { | |
| console.log(i, found, 'was found'); | |
| found.push(i); | |
| } | |
| return found; | |
| }, []) | |
| return ret; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment