Skip to content

Instantly share code, notes, and snippets.

@rubnvp
Created November 12, 2020 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rubnvp/34670e30c29bcd664c83f43f6dc82a52 to your computer and use it in GitHub Desktop.
Save rubnvp/34670e30c29bcd664c83f43f6dc82a52 to your computer and use it in GitHub Desktop.
JavaScript spread operator to conditionally add keys to objects or elements to arrays
const user = {
name: 'John',
...isAdmin && {adminGroup: 'super-admin'},
};
const options = [
'fruits',
...hasFlowers ? ['flowers'] : [],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment