Last active
August 24, 2018 05:23
-
-
Save kmelve/48753be78333d5230f8ea3c1ccae899e to your computer and use it in GitHub Desktop.
How to conditionally build an object in ES6 – 1
This file contains 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
function episodeParser(data) { | |
const { id, | |
title, | |
description, | |
optionalField, | |
anotherOptionalField | |
} = data | |
const parsedEpisode = { guid: id, title, summary: description } | |
if (optionalField) { | |
parsedEpisode.optionalField = optionalField | |
} else if (anotherOptionalField) { | |
parsedEpisode.anotherOptionalField = anotherOptionalField | |
} | |
// and so on | |
return parsedEpisode | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment