Skip to content

Instantly share code, notes, and snippets.

@kmelve
Last active August 24, 2018 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmelve/48753be78333d5230f8ea3c1ccae899e to your computer and use it in GitHub Desktop.
Save kmelve/48753be78333d5230f8ea3c1ccae899e to your computer and use it in GitHub Desktop.
How to conditionally build an object in ES6 – 1
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