Skip to content

Instantly share code, notes, and snippets.

@michaelbramwell
Last active January 14, 2017 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbramwell/74995c5ba1aafe1fcc859ccc747fc816 to your computer and use it in GitHub Desktop.
Save michaelbramwell/74995c5ba1aafe1fcc859ccc747fc816 to your computer and use it in GitHub Desktop.
Find the needle in the haystack
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