Created
June 4, 2018 02:26
-
-
Save frankfaustino/c17f95abfddf64dc03f8ee327a2fe30b to your computer and use it in GitHub Desktop.
reducing an object into an object, array, or int
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 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