CristalT
1/27/2019 - 10:09 PM

Two ways to filter an array of object

// 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)