Created
January 13, 2020 23:03
-
-
Save pfftdammitchris/5b3349352c8aaf76b12659f6a3b5f857 to your computer and use it in GitHub Desktop.
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 people = { | |
bob: { | |
age: 15, | |
gender: 'male', | |
}, | |
jessica: { | |
age: 24, | |
gender: 'female', | |
}, | |
lucy: { | |
age: 11, | |
gender: 'female', | |
}, | |
sally: { | |
age: 14, | |
gender: 'female', | |
}, | |
} | |
const { males, females } = Object.keys(people).reduce( | |
(acc, name) => { | |
const person = people[name] | |
if (person.gender === 'male') { | |
acc.males.push(name) | |
} else { | |
acc.females.push(name) | |
} | |
return acc | |
}, | |
{ males: [], females: [] }, | |
) | |
console.log(males) // ["bob"] | |
console.log(females) // ["jessica", "lucy", "sally"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment