Last active
June 20, 2022 01:09
-
-
Save sktwentysix/01ec5dc9aebb27eebee74ab8ab3ec28b 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 survey = [ | |
{ name: "Zuri", votedFramework: 'react' }, | |
{ name: "Sofia", votedFramework: 'angular' }, | |
{ name: "Elon", votedFramework: 'react' }, | |
{ name: "Joe", votedFramework: 'react' }, | |
{ name: "Alex", votedFramework: 'angular' }, | |
{ name: "Josh", votedFramework: 'vue' }, | |
{ name: "Jade", votedFramework: 'react' }, | |
{ name: "Ted", votedFramework: 'angular' }, | |
{ name: "Leo", votedFramework: 'react' }, | |
{ name: "Nala", votedFramework: 'react' }, | |
{ name: "Jack", votedFramework: 'react' }, | |
{ name: "Fiadh", votedFramework: 'vue' }, | |
]; | |
const reducerCallback = (prevItem, vote, i) => { | |
if (prevItem[vote.votedFramework] === undefined) { | |
return { ...prevItem, [vote.votedFramework]: [vote.name] } | |
} | |
prevItem[vote.votedFramework].push(vote.name) | |
return { ...prevItem } | |
} | |
console.log(survey.reduce(reducerCallback, {})) | |
/* { | |
"react": ["Zuri", "Elon", "Joe", "Jade", "Leo", "Nala", "Jack"], | |
"angular": ["Sofia", "Alex", "Ted"], | |
"vue": ["Josh", "Fiadh"] | |
} */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment