Skip to content

Instantly share code, notes, and snippets.

@CristalT
Created January 27, 2019 22:09
Show Gist options
  • Save CristalT/f460b831e5efeefde380c10354f2ce8f to your computer and use it in GitHub Desktop.
Save CristalT/f460b831e5efeefde380c10354f2ce8f to your computer and use it in GitHub Desktop.
// Two ways to filter an Array of Objects
const heros = [
{ label: 'Batman' },
{ label: 'Iron Man' },
{ label: 'Superman' },
{ label: 'Green Lantern' }
]
const token = 'Superman'
// One Way
const results1 = []
heros.forEach(item => {
if (item.label == token) {
results1.push(item)
}
})
console.log(results1)
// Way Two
const results2 = heros.filter(item => item.label == token)
console.log(results2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment