Last active
October 17, 2019 15:50
-
-
Save alperg/66886b52a72098831f89186ed782529d to your computer and use it in GitHub Desktop.
Add conditional props
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
// In this example, password will be added only when password is truthy! | |
const user = { id: 100, name: 'Howard Moon' }; | |
const password = 'Password!'; | |
const userWithPassword = { | |
...user, | |
id: 100, | |
...(password && { password }) | |
}; | |
console.log(userWithPassword); // { id: 100, name: 'Howard Moon', password: 'Password!' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment