Fuzzy Search
by sbhomra
JavaScript
const items = [
{
name: "Jon Snow",
lastName: "Kit Harington",
},
{
name: "Daenerys Targaryen",
actor: "Emilia Clark",
},
]
const fuzzySearch = (items, query) => {
if (query === null) {
return []
}
var search = query.toLowerCase().split(" ")
var ret = items.reduce((found, i) => {
var matches = 0
search.forEach((s) => {
var props = 0
for (var prop in i) {
if (i[prop].toLowerCase().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
}
console.log(fuzzySearch(items, "d Tar"))