Non-tech founder’s guide to choosing the right software development partner Download Ebook
Home>Blog>M04kqsetdj-add-conditional-properties-in-js-objects

Add conditional properties in JS objects

How conditionaly add properties to object?

As it turned out, it's pretty simple:




const user = { id: 1, name: 'Suhomozgy Andrey' }

const position = 'Frontend Developer'

const userWithPosition = {

  ...user,

  id: 2,

  ...(position && { position })

}



userWithPosition //=> { id: 2, name: 'Suhomozgy Andrey', position: 'Frontend Developer' }





Do you have an idea? Launch it with us!
By submitting this form, you agree with JetRockets’ Privacy Policy