Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Created March 13, 2022 16:34
Show Gist options
  • Select an option

  • Save AitorAlejandro/1923dfe582498ceb27138573d592fb96 to your computer and use it in GitHub Desktop.

Select an option

Save AitorAlejandro/1923dfe582498ceb27138573d592fb96 to your computer and use it in GitHub Desktop.
Conditionally add properties to an object
const condition1 = true;
const condition2 = false;
const person = {
id: 'uuid-1234',
name: 'John',
...(condition1 && { propA: 'A' }),
...(condition2 && { propB: 'B' }),
};
// person --> { id: 'uuid-1234', name: 'John', propA: 'A' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment