Last active
January 14, 2017 13:40
-
-
Save michaelbramwell/74995c5ba1aafe1fcc859ccc747fc816 to your computer and use it in GitHub Desktop.
Find the needle in the haystack
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
let _idxOf = function(needle){ | |
return function(haystack){ | |
return haystack.toLowerCase().indexOf(needle.toLowerCase()) !== -1; | |
}; | |
}; | |
// e.g | |
let searchTerm = "fly"; | |
let pets = [{ name: 'George', type: 'fly' }, { name: 'Benson', type: 'dog' }]; | |
let filtered = pets.filter(function(item){ | |
let f = _idxOf(searchTerm); | |
return f(item.name) || f(item.type); | |
}); | |
console.log(filtered); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment