Last active
March 7, 2018 18:50
-
-
Save dillonchanis/7f581d8ac59528c8612db6ed1b9835bf to your computer and use it in GitHub Desktop.
Deepfilter
This file contains 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 deepFilter (search, arr) { | |
if (!search) return arr | |
const result = [] | |
arr.forEach(element => { | |
let temp = [] | |
let found = false | |
Object.keys(element).forEach(k => { | |
if (String(element[k]).toLowerCase().includes(search.toLowerCase())) { | |
found = true | |
} | |
if (Array.isArray(element[k])) { | |
temp = deepFilter(search, element[k]) | |
if (temp.length) { | |
found = true | |
} | |
} | |
if (found) { | |
result.push(element) | |
} | |
}) | |
}) | |
return [...new Set(result)] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment