Skip to content

Instantly share code, notes, and snippets.

@frankfaustino
Created June 4, 2018 02:26
reducing an object into an object, array, or int
const fruits = {
apple: 'better',
banana: 'good',
peach: 'ok',
pear: 'good'
}
Object
.entries(fruits)
.reduce((goodFruit, [fruit, quality]) => {
if (quality === 'good') goodFruit[fruit] = quality
return goodFruit
}, {})
Object
.entries(fruits)
.reduce((goodFruit, [fruit, quality]) => {
if (quality === 'good') goodFruit.push(fruit)
return goodFruit
}, [])
Object
.values(fruits)
.reduce((goodFruit, quality) =>
quality === 'good' ? (goodFruit + 1) : goodFruit
, 0)
Object
.entries(fruits)
.filter(([fruit, quality]) => quality === 'good')
.map(([fruit, quality]) => fruit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment