Conditionally spread entries to a JavaScript object

Tomasz Łakomy
InstructorTomasz Łakomy

Share this video with your friends

Send Tweet

In JavaScript, we often end up composing one object out of several other objects. Luckily there's a convenient spread operator which allows us to spread entries from one object to another.

Sometimes we only want to include something in the newly created object if a certain condition is met. In this lesson we are going to learn how to conditionally add entries to a JavaScript object using the spread operator.

Wojciech Morkowski
Wojciech Morkowski
~ 2 years ago

What about: {...(isAdmin() && { is_admin: true })} ? ;)