Created
January 17, 2019 19:59
-
-
Save CristalT/5d1b07052d86da2e47a2e06413d5ff51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const heros = [ | |
{ label: 'Batman' }, | |
{ label: 'Iron Man' }, | |
{ label: 'Superman' }, | |
{ label: 'Green Lantern' }, | |
{ label: 'Cat Woman' }, | |
{ label: 'Supergirl' }, | |
{ label: 'Aquaman' } | |
] | |
const terms = 'Man Super' // unordered terms for searching | |
const search = terms.toLowerCase().split(' ') | |
console.log(search) | |
// Way One | |
const resultsOne = [] | |
heros.forEach(item => { | |
if (search.every(word => item.label.toLowerCase().search(word) !== -1)) { | |
resultsOne.push(item) | |
} | |
}) | |
console.log(resultsOne) | |
// Way Two | |
const resultsTwo = heros.filter(item => search.every(word => item.label.toLowerCase().indexOf(word) !== -1)) | |
console.log(resultsTwo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment