Skip to content

Instantly share code, notes, and snippets.

@evanwinter
Created June 3, 2019 20:57
Show Gist options
  • Select an option

  • Save evanwinter/ca4a44b16a3435356953c05e34883796 to your computer and use it in GitHub Desktop.

Select an option

Save evanwinter/ca4a44b16a3435356953c05e34883796 to your computer and use it in GitHub Desktop.
Take an array of objects, and return an object containing those objects, indexable by a property.
const input = [
{
"id": "b297ed8f-ad81-5988-a08e-b4ec51428981",
"questionBody": "What generations are you primarily serving?",
"questionAnswers": [
"Gen Z (1997-on)",
"Millennials (1981-1997)",
"Gen X (1965-1980)",
"Boomers (1946-1964)"
],
"isMultipleChoice": true
},
{
"id": "3de3a29d-e7a5-5117-a2fe-a619d73496db",
"questionBody": "How many fry varieties do you offer on your menu?",
"questionAnswers": [
"1",
"2",
"3",
"4+"
],
"isMultipleChoice": false
},
{
"id": "f4b4ffc1-af00-56e8-9435-110cc91e370b",
"questionBody": "How do you serve fries on your menu?",
"questionAnswers": [
"Side",
"Shareable",
"Snack",
"Appetizer",
"Entree",
"Other",
"None of the above"
],
"isMultipleChoice": true
},
{
"id": "c08ef3ce-7622-503f-8afd-914ce963f1d8",
"questionBody": "How often do you update your menu?",
"questionAnswers": [
"Daily",
"Weekly",
"Monthly",
"Yearly",
"Never"
],
"isMultipleChoice": false
}
]
const tagged = questions.reduce((acc, curr) => {
return {...acc, ...{[curr.id]: curr}}
}, {})
// now, we can things like this:
const myObjID = input[0].id
return tagged[myObjID]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment