Skip to content

Instantly share code, notes, and snippets.

@trungpv1601
Created August 2, 2019 12:33
Show Gist options
  • Select an option

  • Save trungpv1601/5cb693cbc31a79251046ca60cd2de8c0 to your computer and use it in GitHub Desktop.

Select an option

Save trungpv1601/5cb693cbc31a79251046ca60cd2de8c0 to your computer and use it in GitHub Desktop.
Filter JavaScript Objects with Fuzzy Search
// 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