Created
January 27, 2019 22:09
-
-
Save CristalT/f460b831e5efeefde380c10354f2ce8f to your computer and use it in GitHub Desktop.
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
// 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